Java異常處理的五個關鍵字,java異常處理關鍵字

來源:互聯網
上載者:User

Java異常處理的五個關鍵字,java異常處理關鍵字

異常:異常有的是因為使用者錯誤引起,有的是程式錯誤引起的,還有其它一些是因為物理錯誤引起的。

異常處理關鍵字:try、catch、finally、throw、throws

注意事項:

  1. 錯誤不是異常,而是脫離程式員控制的問題。
  2. 所有的異常類是從 java.lang.Exception 類繼承的子類。
  3. 異常類有兩個主要的子類:IOException 類和 RuntimeException 類。
  4. Java有很多的內建異常類。

異常大致分類:

  1. 使用者輸入了非法資料。
  2. 要開啟的檔案不存在。
  3. 網路通訊時串連中斷,或者JVM記憶體溢出。

文法:

try{//需要監聽的代碼塊}
catch(異常類型 異常名稱/e){//對捕獲到try監聽到的出錯的代碼塊進行處理throw 異常名稱/e; //thorw表示拋出異常throw new 異常類型(“自訂”);}

finally{//finally塊裡的語句不管異常是否出現,都會被執行}修飾符 傳回值 方法名 () throws 異常類型{ //throws只是用來聲明異常,是否拋出由方法調用者決定//代碼塊}

代碼例子:(try與catch與finally)

public class ExceptionTest {public static void main(String[] args) {Scanner input=new Scanner(System.in);     try{ //監聽代碼塊     int a=input.nextInt();     int b=input.nextInt();     double sum=a/b;      System.out.println(sum);     }     catch(InputMismatchException e){       System.out.println("只能輸入數字");     }     catch(ArithmeticException e){       System.out.println("分母不能為0");     }     catch(Exception e){ //Exception是所有異常的父類       System.out.println("發生了其他異常");     }     finally{ //不管是否出現異常,finally一定會被執行       System.out.println("程式結束");     } }}

代碼例子:(throw關鍵字)

import java.util.InputMismatchException;import java.util.Scanner;public class ExceptionTest {public static void main(String[] args) {Scanner input=new Scanner(System.in);     try{ //監聽代碼塊     int a=input.nextInt();     int b=input.nextInt();     double sum=a/b;      System.out.println(sum);     }     catch(InputMismatchException e){ //catch(異常類型 異常名稱)       System.out.println("只能輸入數字");       throw e; //拋出catch捕捉到的異常       //throw new InputMismatchException(); 同上     }     catch(ArithmeticException e){       System.out.println("分母不能為0");       throw new ArithmeticException("分母為0拋出異常"); //拋出ArithmeticException異常     }     catch(Exception e){ //Exception是所有異常的父類       System.out.println("發生了其他異常");     }     finally{ //不管是否出現異常,finally一定會被執行       System.out.println("程式結束");     }  }}

代碼例子:(throws)

public class Throws {int a=1;int b=0;public void out() throws ArithmeticException{ //聲明可能要拋出的異常,可以有多個異常,逗號隔開try{ //監聽代碼塊int sum=a/b;System.out.println(sum);}catch(ArithmeticException e){System.out.println("分母不能為0");}finally{ //不管是否出現異常,finally一定會被執行System.out.println("程式結束");}}public static void main(String[] args){Throws t=new Throws();t.out(); //調用方法throw new ArithmeticException("分母為0拋出異常"); //由調用的方法決定是否要拋出異常/* * 第二種拋出方式 *///ArithmeticException a=new ArithmeticException("分母為0拋出異常");//throw a;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.