# 異常處理
[TOC]
## 為什么會出現異常
>[danger] 用戶不正當的輸入
> 本身系統的問題
## 為什么要處理異常
~~~
public class ExceptionStudy {
public static void main(String[] args) {
System.out.println("******程序開始運行*******");
System.out.println("******數學計算:"+ (10/2) +"*******");
System.out.println("******程序運行結束*******");
}
}
運行結果:
******程序開始運行*******
******數學計算:5*******
******程序運行結束*******
public class ExceptionStudy {
public static void main(String[] args) {
System.out.println("******程序開始運行*******");
System.out.println("******數學計算:"+ (10/0) +"*******");
System.out.println("******程序運行結束*******");
}
}
運行結果:
******程序開始運行*******
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.ntdodoke.preparation.usuallyClass.ExceptionStudy.main(ExceptionStudy.java:6)
~~~
如果不處理異常,一旦在出現異常的地方,程序就會被「異常指令流」直接終止,不再往下運行。為了讓程序在出現異常后依然可以正常執行,所以我們必須正確處理異常來完善代碼的編寫。
## 處理異常
**使用 try catch**
>[success] 判斷一個對象是否為空,使用`null != str`
在程序方法設計,特別是方法的實現中,對于傳入的參數,是不可知的,所以要對傳入的參數進行最基礎的驗證后,才能使用,例如,判斷是否為 null。盡量猜測可能會發生的問題,確保異常的少發生和不會發生
~~~
public static void main(String[] args) {
fun1("",null);
fun2();
}
public static void fun1(String str, String subStr) {
if (null != str && null != subStr) {
if(str.indexOf(subStr) >= 0) {
System.out.println("存在子串");
} else {
System.out.println("不存在子串");
}
} else {
System.out.println("不合法字符串");
}
}
public static void fun2() {
System.out.println("++++++++++++++");
System.out.println("繼續執行");
System.out.println("++++++++++++++");
}
~~~
在 try catch 結構中,catch 是可以省略的,也可以多異常的捕獲,但是出現多異常的時候,父類異常只能出現在最下面。
在實際的開發中,一般最簡單的方式就是使用一個 Exception 直接處理。
~~~
public class ExceptionStudy {
public static void main(String[] args) {
fun1("",null);
fun2();
}
public static void fun1(String str, String subStr) {
try{
if (str.indexOf(subStr) >= 0) {
System.out.println("存在子串");
} else {
System.out.println("不存在子串");
}
int a = 10/0;
} catch(NullPointerException e) {
System.out.println("出現空指針異常");
System.out.println("輸入非法字符");
//e.toString(): 獲得異常種類和錯誤信息
//e.getMessage():獲得錯誤信息
//e.printStackTrace():在控制臺打印出異常種類,錯誤信息和出錯位置等
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.toString());
} catch(ArithmeticException e1) {
e1.printStackTrace();
} finally {
System.out.println("finally中的內容無論發不發生異常都會執行");
}
}
public static void fun2() {
System.out.println("++++++++++++++");
System.out.println("繼續執行");
System.out.println("++++++++++++++");
}
}
~~~
**使用 finally**
無論是否出現異常都會被執行,特別要注意的是,如果沒有寫 catch ,那么 finally 是會被執行的,但是中斷后的語句是不會被執行的。
~~~
public class ExceptionStudy {
public static void main(String[] args) {
fun1("",null);
fun2();
}
public static void fun1(String str, String subStr) {
try{
if (str.indexOf(subStr) >= 0) {
System.out.println("存在子串");
} else {
System.out.println("不存在子串");
}
} finally {
System.out.println("finally中的內容無論發不發生異常都會執行");
}
}
public static void fun2() {
System.out.println("++++++++++++++");
System.out.println("繼續執行");
System.out.println("++++++++++++++");
}
}
~~~
**throws**
在**方法定義**的后面顯式的聲明方法是有異常的,調用該方法的程序是要顯式的處理異常,后者也可以再次拋出。
~~~
public class ExceptionStudy {
public static void main(String[] args) {
try {
fun3();
} catch(NullPointerException e) {
e.printStackTrace();
}
fun2();
}
public static void fun1(String str, String subStr) throws NullPointerException {
if(str.indexOf(subStr) >= 0) {
System.out.println("存在子串");
} else {
System.out.println("不存在子串");
}
}
public static void fun2() {
System.out.println("**********************");
System.out.println("繼續執行");
System.out.println("**********************");
}
public static void fun3() throws NullPointerException {
fun1("aaa",null);
System.out.println("aaaaaaa");
}
}
~~~
**throw**
自定義的創建一個異常對象。在一般的異常發生時候,虛擬機會動態的創建一個「系統異常」拋出,和自定義使用 throw 拋出是一個概念。
~~~
public class Demo3 {
public static void main(String[] args) throws Exception {
fun1(17);
System.out.println("end");
}
public static void fun1(int age) throws Exception {
if (age < 18) {
throw new Exception("您的年齡不合法");
} else {
System.out.println("您已經進入VIP房間");
}
}
}
~~~
`throws`和`throw`的區別
throws是指方法可能會發生異常
throw則是通過語句拋出異常,當執行throw語句的時候異常已經發生了
## 異常堆棧信息
* 異常類
* 異常提示信息:getMessage()
* 異常所在的代碼位置:自下而上是異常出現的代碼所在方法的調用順序的先后。
## 常見的異常
NPE:NullPointerException 在調用對象屬性或者方法的時候,對象其實是個 null,就會報此異常;
java.lang.ArrayIndexOutOfBoundsException:數組越界
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1:集合越界