一、java異常總結:
異常就是程序運行時出現不正常運行情況
1.異常由來:
通過java的類的形式對現實事物中問題的描述,并封住成了對象
其實就是java對不正常情況描述后的對象體現
2.對于問題的劃分有兩種:一種是嚴重的問題,一種是非嚴重的問題
對于嚴重的,java通過Error類來描述
對于Error一般不編寫針對性的代碼對其進行處理
對于非嚴重的,java通過Exception類來描述
對于Exception可以使用針對性的處理方式進行處理
3.常見的異常有:數組角標越界異常,空指針異常……
4.無論Error或者Exception都有一些共性的內容。
比如:不正常情況的消息,引發原因等。
Throwable //父類(下面兩個類相同的共性抽取出來的)
|--Error
|--Excption //兩個子類(里面定義了很多問題(異常出現)) /*父類名作為子類后綴名*/
實例1:出現異常示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Demo { public int div( int x, int y) { return x/y; } } class ExceptionDemo { public static void main(String args[]) { Demo d= new Demo(); int x=d.div( 4 , 0 ); //0作為除數 System.out.println( "x=" +x); System.out.println( "over" ); } } |
運行結果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.div(ExceptionDemo.java:5)
at ExceptionDemo.main(ExceptionDemo.java:15)
從上面的結果可以分析出,在第5和第15行都出現了異常,這是因為除法的機制,除數不能為0,這時候運行就拋出了異常。
實例2:出現異常示例2,內存溢出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Demo { public int div( int x, int y) { return x/y; } } class ExceptionDemo { public static void main(String args[]) { /*Demo d=new Demo(); int x=d.div(4,0); System.out.println("x="+x); System.out.println("over"); */ byte [] arr= new byte [ 1024 * 1024 * 1000 ]; } } |
運行結果:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at ExceptionDemo.main(ExceptionDemo.java:19)
java.lang.OutOfMemoryError:代表內存溢出異常
二、異常的處理:
對于異常的處理,java提供了特有的語句進行處理
格式
try
{
需要被檢測的代碼;
}
catch
{
處理異常的代碼;(處理方式)
}
finally
{
一定會執行的代碼;(處理方式)
}
實例1:演示try catch語句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class Demo { public int div( int x, int y) { return x/y; } } class ExceptionDemo { public static void main(String args[]) { Demo d= new Demo(); try { int x=d.div( 4 , 0 ); System.out.println( "x=" +x); } catch (Exception e) { System.out.println( "除數有誤" ); } System.out.println( "over" ); /*byte[] arr=new byte[1024*1024*1000];*/ } } |
運行結果:
除數有誤
over
結果分析:程序在運行時,當執行到除法的語句:return x/y時,就生成了異常的對象 new AritchmeticException(),try語句把這個對象讓catch語句的參數捕獲
Exception e =new AritchmeticException();
運行完catch的處理語句后,問題就被處理完了,結束語句,輸出over
實例2:對捕獲到的異常對象進行常見的方法操作(父類Throwable的方法)
String getMessage(); //獲取異常信息
toString() //返回異常名稱:異常信息
printStackTrace() //輸出異常名稱,異常信息,異常出現的位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Demo { public int div( int x, int y) { return x/y; } } class ExceptionDemo { public static void main(String args[]) { Demo d= new Demo(); try { int x=d.div( 4 , 0 ); System.out.println( "x=" +x); } catch (Exception e) { System.out.println( "除數有誤" ); //獲得異常信息 System.out.println(e.getMessage()); //獲得異常信息,異常名稱 System.out.println(e.toString()); //輸出異常名稱,異常信息,異常出現的位置 e.printStackTrace(); } System.out.println( "over" ); /*byte[] arr=new byte[1024*1024*1000];*/ } } |
運行結果:
除數有誤
/ by zero
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
at Demo.div(ExceptionDemo.java:5)
at ExceptionDemo.main(ExceptionDemo.java:17)
over
從運行結果分析,其實jvm默認異常處理機制就是在調用printStackTrace方法。
實例3:拋出異常的兩種處理方式
1.拋出給jvm虛擬機處理
2.拋出的異常自己處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Demo { public int div( int x, int y) throws Exception /*有可能出現異常的地方拋出異常*/ { return x/y; } } class ExceptionDemo { public static void main(String args[]) { Demo d= new Demo(); int x=d.div( 4 , 0 ); System.out.println( "x=" +x); System.out.println( "over" ); } } |
運行結果:
ExceptionDemo.java:15: 錯誤: 未報告的異常錯誤Exception; 必須對其進行捕獲或聲明以
便拋出
int x=d.div(4,0);
^
1 個錯誤
結果分析:這是因為沒有對有可能出現異常進行處理
處理方式1:不斷拋出異常,讓jvm虛擬機自己處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Demo { public int div( int x, int y) throws Exception /*有可能出現異常的地方拋出異常*/ { return x/y; } } class ExceptionDemo { public static void main(String args[]) throws Exception /*繼續拋出異常,給虛擬機*/ { Demo d= new Demo(); int x=d.div( 4 , 0 ); System.out.println( "x=" +x); System.out.println( "over" ); } } |
處理方式2:自己處理異常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
class Demo { public int div( int x, int y) throws Exception /*有可能出現異常的地方拋出異常*/ { return x/y; } } class ExceptionDemo { public static void main(String args[]) { Demo d= new Demo(); try //自己處理異常 { int x=d.div( 4 , 0 ); System.out.println( "x=" +x); } catch (Exception e) { System.out.println( "除數有誤" ); //獲得異常信息,異常名稱 System.out.println(e.toString()); System.out.println( "over" ); } } } |
總結:
在函數上聲明異常。便于提高安全性,讓調出處進行處理,不處理編譯失敗。
實例4:對多異常處理
1.聲明異常時,建議聲明更為具體的異常,這樣處理得可以更具體
2.聲明幾個異常,就對應有幾個catch塊,不要定義多余的catch快。
如果有多個catch塊中的異常出現繼承關系,父類異常catch塊放在下面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
class Demo { public int div( int x, int y) throws ArithmeticException,ArrayIndexOutOfBoundsException { int arr[]= new int [x]; System.out.println(arr[ 4 ]); return x/y; } } class ExceptionDemo { public static void main(String args[]) { Demo d= new Demo(); try { int x=d.div( 4 , 0 ); System.out.println( "x=" +x); } catch (ArithmeticException e) /*除法法則異常對象接收,第一個執行*/ { System.out.println("除數有誤"); //獲得異常信息,異常名稱 System.out.println(e.toString()); System.out.println("over"); } catch(ArrayIndexOutOfBoundsException e) /*數據越界的對象接收,第二個執行*/ { System.out.println("數組越界了"); //輸出異常信息 System.out.println(e.toString()); } catch(Exception e) /*父類Exception接收,最后執行,建議不要寫這個,讓程序終止*/ /*用到了多態*/ { System.out.println(e.toString()); } } } |
運行結果:
數組越界了
java.lang.ArrayIndexOutOfBoundsException: 4
建議:
建立在catch處理時,catch中一定要定義具體的處理方式
不要簡單定義一句 e.printStackTrace().
也不要簡單就書寫一條輸出語句
因為用戶看不懂,最好保存到文件中,定時發給我們開發者去查看。
實例5:自定義異常
你們有沒有發現,我們正在使用的異常都是java中封裝好的
但在實際開發中,我們的程序中出現的異常,有可能是java沒有封裝的,
這時候,就需要自己定義了
我根據上面的代碼,定義除數不能為負數,代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
class Demo { public int div( int x, int y) throws FuShuException /*拋出異常*/ { if(y<0) { throw new FuShuException("分母出現負數了------/bu FuShu",y); /*自己手動拋出異常的對象*/ } return x/y; } } class FuShuException extends Exception { private int value; FuShuException(String m,int value) { super(m); /*給父類Exception的getMessage方法傳遞參數*/ this.value=value; } public int getValue() /*自定義的方法,返回負數*/ { return value; } } class ExceptionDemo { public static void main(String args[]) { Demo d=new Demo(); try { int x=d.div(4,-3); System.out.println("x="+x); } catch(FuShuException e) /*捕獲異常對象*/ { System.out.println(e.getMessage()+e.getValue()); } System.out.println( "over" ); } } |
運行結果:
分母出現負數了------/bu FuShu-3
over
從上面的結果,可以看出
在本程序中,對于除數是-3,也視為是錯誤的是無法進行運算的。
那么就需要對這個問題進行自定義的描述。
當在函數內部出現了throw拋出異常對象,那么就必須要給對應的處理動作。
要么在內部try catch處理。
要么在函數上聲明讓調用者處理。
一般情況在,函數內出現異常,函數上需要聲明。
發現打印的結果中只有異常的名稱,卻沒有異常的信息。
因為自定義的異常并未定義信息。
如何定義異常信息呢?
因為父類中已經把異常信息的操作都完成了。
所以子類只要在構造時,將異常信息傳遞給父類通過super語句。
那么就可以直接通過getMessage方法獲取自定義的異常信息。
自定義異常必須是自定義類繼承Exception。
繼承Exception原因:
異常體系有一個特點:因為異常類和異常對象都被拋出。
他們都具備可拋性。這個可拋性是Throwable這個體系中獨有特點。
只有這個體系中的類和對象才可以被throws和throw操作。
throws和throw的區別
throws使用在函數上。
throw使用在函數內。
throws后面跟的異常類。可以跟多個。用逗號隔開。
throw后跟的是異常對象。
實例6:Exception中有一個特殊的子類異常RuntimeException 運行時異常
如果在函數內容拋出該異常,函數上可以不聲明,編譯一樣通過。
如果函數上聲明了該異常,調用者可以不進行處理,編譯一樣通過
之所以不用在函數聲明,是因為不需要讓調用者處理
當該異常發生,希望程序停止,因為在運行時,出現了無法運行的情況,希望程序停止后
程序員對該代碼進行修改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
class Demo { public int div( int x, int y) throws FuShuException /*拋不拋結果都一樣*/ { if(y<0) { throw new FuShuException("分母出現負數了------/bu FuShu",y); } return x/y; } } class FuShuException extends RuntimeException /*繼承RuntimeException*/ { FuShuException(String m,int value) { super(m); } } class ExceptionDemo { public static void main(String args[]) { Demo d=new Demo(); int x=d.div(4,-3); /*運行到這會出現異常,編譯沒有問題*/ System.out.println( "x=" +x); System.out.println( "over" ); } } |
運行結果:
Exception in thread "main" FuShuException: 分母出現負數了------/bu FuShu
at Demo.div(ExceptionDemo.java:7)
at ExceptionDemo.main(ExceptionDemo.java:26)
從上面的結果可以看出:
自定義異常時:如果該異常的發生,無法在繼續進行運算,
就讓自定義異常繼承RuntimeException。
對于異常分兩種:
1,編譯時被檢測的異常。
2,編譯時不被檢測的異常(運行時異常。RuntimeException以及其子類)
以上這篇全面理解java中的異常處理機制就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。