異常處理機制

1,異常

      異常的定義:就是程序在運行時出現不正常情況。

      對於嚴重的異常:Java通過Error類進行描述。對於Error一般不編寫針對性的代碼對其進行處理。

      對於非嚴重的異常:Java通過Exception類進行描述。對於Exception可以使用針對性的處理方式進行處理。

2,運用Java的異常處理機制

      Java提供兩種異常處理機制

       (1)用try...catch語句捕獲並且處理異常;

     (2)使用throw拋出異常,異常必須是java.lang.Throwable類的對象或者該類的子類的對象;

     (3)使用throws聲明方法拋出異常;

     (4)使用finally語句聲明在任何情況下都會執行的代碼。

3,try...catch的用法:

try{
 需要被檢測的代碼;
}
catch(異常類   變量){
 處理異常的代碼;(處理方式)
}
finally{
 一定會執行的語句;
}

    (1)try不能脫離catch或者finally而單獨存在;

    (2)try後面可以跟0個或者多個catch,還可以有至多0個或者多個finally;

    (3)try後面可以只跟finally代碼塊;

    (4)在try代碼塊中定義的變量的作用域只能夠用於try代碼塊, 在catch代碼塊和finally代碼塊中不能夠訪問該變量;

    (5)當try代碼塊後面跟若干個catch塊時,JVM會一次匹配異常情況,如果拋出的異常類型是catch處理異常類型或者是其子類型,則執行相應的catch代碼塊,之後的catch塊不會再執行;

    (6)如果一個方法可能拋出受檢查異常,要麼使用try...catch進行捕獲和處理,或者使用throws聲明拋出異常;

4,throw和throws的區別:

    (1)throws使用在函數上,throw使用在函數內;

    (2)throws後面跟的異常類,可以跟多個,用逗號隔開;throw後跟的是異常對象;

     throws拋出異常交給調用該方法的方法處理,即:

public class Test {
 public static void main(String[] args) {
  Test2 test2 = new Test2();
  try {
   System.out.println("invoke the method begin!");
   test2.method();
   System.out.println("invoke the method end!");
  } catch (Exception e) {
   System.out.println("catch Exception!");
  }
 }
}

class Test2 {
 public void method() throws Exception {
  System.out.println("method begin!");
  int a = 10;
  int b = 0;
  int c = a / b;
  System.out.println("method end!");
 }
}

運行結果:

invoke the method begin!
method begin!
catch Exception!

    throw語句拋出異常。throw後的語句沒有機會執行。

public class Test {
 public static void main(String[] args) {
  Test2 test2 = new Test2();
  try {
   System.out.println("invoke the method begin!");
   test2.method();
   System.out.println("invoke the method end!");
  } catch (Exception e) {
   System.out.println("catch Exception!");
  }
 }
}

class Test2 {
 public void method() {
  System.out.println("method begin!");
  throw new ArrayIndexOutOfBoundsException();
 }
}

運行結果:

invoke the method begin!
method begin!
catch Exception!

5,創建自己的異常

class FuShuException extends Exception{//自定義異常
 private int value;
 public FuShuException(String msg,int value) {
  super(msg);
  this.value = value;
 }
 public int getValue(){
  return value;
 }
}
class Demo{
 int div(int a,int b)throws FuShuException{
  if(b<0)
   throw new FuShuException("出現了除數是負數的情況 / by fushu",b);//手動通過throw關鍵字拋出一個自定義異常對象。
  return a/b;
 }
}
public class ExceptionDemo1 {
 public static void main(String[] args) {
  Demo d = new Demo();
  try {
   int x = d.div(5, -1);
  } catch (FuShuException e) {
   System.out.println(e.getMessage());
   System.out.println("錯誤的負數是:"+e.getValue());
  }
 }
}

運行結果:

出現了除數是負數的情況 / by fushu
錯誤的負數是:-1

6,.受檢查異常和運行時異常

        (1)運行時異常

           拋出一個異常(throw 一個異常的對象)沒有用try...catch捕獲並處理,也沒有用throws進行聲明,這樣的錯誤在編譯時並不會檢查出來,在運行時纔會出現運行時的異常。

        (2)受檢查異常

           除了運行時異常,其他異常均是受檢查異常,特點是如果throw一個異常對象,如果沒有使用try...catch捕獲並處理,並且沒有使用throws對異常進行聲明,則會出現編譯錯誤。

              Java中常見的運行時異常:IllegalArgumentException(無效參數異常)、ClassCastException(數據類型轉換錯誤)、IndexOutOfBoundException(下角標越界異常)、NullPointerException(空指針異常)、ArithmeticException(算術異常)。

發佈了22 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章