1. 程式人生 > >java異常的基本概念

java異常的基本概念

異常
異常是導致程式中斷你的執行的一種指令流,如果不對異常進行正確的處理,則可能導致程式的中斷執行,造成不必要的損失,所以在程式的設計中必須要考慮到各種異常的發生,並正確的做好相應的處理,這樣才能保證程式的正確執行

public class ExceptionDemo01 {
public static void main(String[] args) {
System.out.println(“*計算開始***“);
int i = 10;
int j = 0;
int temp = i/j; //此處產生異常
System.out.println(temp);
System.out.println(“*計算結束***

“);
}
}
執行結果:*計算開始***
Exception in thread “main” java.lang.ArithmeticException: / by zero
at Exp.ExceptionDemo01.main(ExceptionDemo01.java:8)
一旦產生異常之後,異常之後的語句並不會執行,而是直接結束程式,並將錯誤報告給使用者
在計算機的發展史有兩大殺手
*斷電
*被除數為0

處理異常
try{
//有可能出現異常的語句
}catch(異常類異常物件){
//編寫異常的處理物件
}catch(異常類的處理語句){
//編寫異常類的處理語句
}catch(異常類的處理語句){
//編寫異常類的處理語句
}finally{
//一定要執行到的程式程式碼

public class ExceptionDemo02 {
public static void main(String[] args) {
System.out.println(“*計算開始***“);
int i = 10;
int j = 0;
try {
int temp = i/j; //此處產生異常
System.out.println(temp);
System.out.println(“——————–”);
}catch(ArithmeticException e) {
System.out.println(“出現異常了”);
}

    System.out.println("****計算結束******");
}

}
執行效果:*計算開始***
出現異常了
*計算結束***

以上的操作程式碼只使用了基本的異常處理格式:try…catch,try中捕獲異常,出現異常之後的程式碼將不再被執行。而是跳轉到相應的catch語句中,用於處理異常。
當然,對於異常也可以設定其同意的出口,使用finally完成。

public class ExceptionDemo03 {
public static void main(String[] args) {
System.out.println(“*計算開始***“);
int i = 10;
int j = 0;
try {
int temp = i/j; //此處產生異常
System.out.println(temp);
System.out.println(“——————–”);
}catch(ArithmeticException e) { //捕獲算數異常
System.out.println(“出現異常了” + e);
}finally { //作為異常的統一出口
System.out.println(“不管處沒出現異常,都將執行此程式碼”);
}
System.out.println(“*計算結束***“);
}
}
執行結果:
*計算開始***
出現異常了java.lang.ArithmeticException: / by zero
不管處沒出現異常,都將執行此程式碼
*計算結束***

以上的程式只是在程式碼中處理了一個異常,如果現在有多個異常呢?
修改程式碼:修改輸入i和j的內容
public class ExceptionDemo04 {
public static void main(String[] args) {
System.out.println(“*計算開始***“);
int i = 0;
int j = 0;
try {
String str1 = args[1]; //接受第一個引數
String str2 = args[2]; //接受第二個引數
i = Integer.parseInt(str1); //將第一個引數由字串轉化為整形
j = Integer.parseInt(str2); //將第二個引數由字串轉化為整形
int temp = i/j; //此處產生異常
System.out.println(temp);
System.out.println(“——————–”);
}catch(ArithmeticException e) { //捕獲算數異常
System.out.println(“出現異常了” + e);
}finally { //作為異常的統一出口
System.out.println(“不管處沒出現異常,都將執行此程式碼”);
}
System.out.println(“*計算結束***“);
}
}

上述程式碼中出現三個異常:
1.如果沒有輸入引數或者輸入的引數不夠,會出現問題。
2.如果現在輸入的時候輸入的不是數字,而是字母。
3.算數異常,如果被除數為0的話
如果想要保證程式的執行正確,則必須同時對三個異常進行處理,所以此時需要三個catch語句
public class ExceptionDemo05 {
public static void main(String[] args) {
System.out.println(“*計算開始***“);
int i = 0;
int j = 0;
try {
String str1 = args[1]; //接受第一個引數
String str2 = args[2]; //接受第二個引數
i = Integer.parseInt(str1); //將第一個引數由字串轉化為整形
j = Integer.parseInt(str2); //將第二個引數由字串轉化為整形
int temp = i/j; //此處產生異常
System.out.println(“兩個數字相除的結果:” + temp);
System.out.println(“——————–”);
}catch(ArithmeticException e) { //捕獲算數異常
System.out.println(“算數異常” + e);
}catch(NumberFormatException e) { //捕獲數字轉換異常
System.out.println(“數字轉換異常:” + e);
}catch(ArrayIndexOutOfBoundsException e) { //捕獲陣列越界異常
System.out.println(“陣列越界異常:” + e);
}
System.out.println(“*計算結束***“);
}
}
此程式碼完全解決了上述三個問題,當然程式還有可能存在其他異常。

異常類的繼承結構
在整個java的異常結構中,實際上有以下兩個最常用的類:Exception、Error,這兩個類全都是Throwable的子類。
*Exception:一般表示的是程式中出現的問題,可以直接使用try…catch處理
*Error:一般指的是jvm的錯誤,程式中無法處理。
注意:
一般在輸出異常資訊的時候,可以直接使用syso列印異常物件
也可以通過Exception提供的一個方法:public void PrintStackTrace()

java的異常處理機制
在整個java異常處理中,實際上也是按照面向物件的方式進行處理,處理的步驟如下:
*一旦產生異常,則首先會產生一個異常類的例項化物件
*在try語句中對此異常物件進行捕捉
*產生的異常物件與catch語句中的各個異常型別進行匹配,如果匹配成功,則執行catch語句中的程式碼

通過對物件的多型性瞭解,子類的例項化物件可以直接使用父類的物件進行接受。
那麼在異常處理中也是此種概念,因為try中產生的是一個例項化物件。如果現在有一些其他的異常沒有被捕獲,則可以使用Exception進行捕獲。
public class ExceptionDemo06 {
public static void main(String[] args) {
System.out.println(“*計算開始***“);
int i = 0;
int j = 0;
try {
String str1 = args[1]; //接受第一個引數
String str2 = args[2]; //接受第二個引數
i = Integer.parseInt(str1); //將第一個引數由字串轉化為整形
j = Integer.parseInt(str2); //將第二個引數由字串轉化為整形
int temp = i/j; //此處產生異常
System.out.println(“兩個數字相除的結果:” + temp);
System.out.println(“——————–”);
}catch(ArithmeticException e) { //捕獲算數異常
System.out.println(“算數異常” + e);
}catch(NumberFormatException e) { //捕獲數字轉換異常
System.out.println(“數字轉換異常:” + e);
}catch(ArrayIndexOutOfBoundsException e) { //捕獲陣列越界異常
System.out.println(“陣列越界異常:” + e);
}catch(Exception e) {
System.out.println(“其他異常:” + e);
}
System.out.println(“*計算結束***“);
}
}
注意點:
在異常處理中,捕獲更粗的異常要放在捕獲更細的異常之前。