一、java異常總結(jié):
異常就是程序運(yùn)行時(shí)出現(xiàn)不正常運(yùn)行情況
1.異常由來(lái):
通過(guò)java的類的形式對(duì)現(xiàn)實(shí)事物中問(wèn)題的描述,并封住成了對(duì)象
其實(shí)就是java對(duì)不正常情況描述后的對(duì)象體現(xiàn)
2.對(duì)于問(wèn)題的劃分有兩種:一種是嚴(yán)重的問(wèn)題,一種是非嚴(yán)重的問(wèn)題
對(duì)于嚴(yán)重的,java通過(guò)Error類來(lái)描述
對(duì)于Error一般不編寫針對(duì)性的代碼對(duì)其進(jìn)行處理
對(duì)于非嚴(yán)重的,java通過(guò)Exception類來(lái)描述
對(duì)于Exception可以使用針對(duì)性的處理方式進(jìn)行處理
3.常見(jiàn)的異常有:數(shù)組角標(biāo)越界異常,空指針異常……
4.無(wú)論Error或者Exception都有一些共性的內(nèi)容。
比如:不正常情況的消息,引發(fā)原因等。
Throwable //父類(下面兩個(gè)類相同的共性抽取出來(lái)的)
|--Error
|--Excption //兩個(gè)子類(里面定義了很多問(wèn)題(異常出現(xiàn))) /*父類名作為子類后綴名*/
實(shí)例1:出現(xiàn)異常示例
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作為除數(shù) System.out.println( "x=" +x); System.out.println( "over" ); } } |
運(yùn)行結(jié)果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.div(ExceptionDemo.java:5)
at ExceptionDemo.main(ExceptionDemo.java:15)
從上面的結(jié)果可以分析出,在第5和第15行都出現(xiàn)了異常,這是因?yàn)槌ǖ臋C(jī)制,除數(shù)不能為0,這時(shí)候運(yùn)行就拋出了異常。
實(shí)例2:出現(xiàn)異常示例2,內(nèi)存溢出
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 ]; } } |
運(yùn)行結(jié)果:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at ExceptionDemo.main(ExceptionDemo.java:19)
java.lang.OutOfMemoryError:代表內(nèi)存溢出異常
二、異常的處理:
對(duì)于異常的處理,java提供了特有的語(yǔ)句進(jìn)行處理
格式
try
{
需要被檢測(cè)的代碼;
}
catch
{
處理異常的代碼;(處理方式)
}
finally
{
一定會(huì)執(zhí)行的代碼;(處理方式)
}
實(shí)例1:演示try catch語(yǔ)句
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( "除數(shù)有誤" ); } System.out.println( "over" ); /*byte[] arr=new byte[1024*1024*1000];*/ } } |
運(yùn)行結(jié)果:
除數(shù)有誤
over
結(jié)果分析:程序在運(yùn)行時(shí),當(dāng)執(zhí)行到除法的語(yǔ)句:return x/y時(shí),就生成了異常的對(duì)象 new AritchmeticException(),try語(yǔ)句把這個(gè)對(duì)象讓catch語(yǔ)句的參數(shù)捕獲
Exception e =new AritchmeticException();
運(yùn)行完catch的處理語(yǔ)句后,問(wèn)題就被處理完了,結(jié)束語(yǔ)句,輸出over
實(shí)例2:對(duì)捕獲到的異常對(duì)象進(jìn)行常見(jiàn)的方法操作(父類Throwable的方法)
String getMessage(); //獲取異常信息
toString() //返回異常名稱:異常信息
printStackTrace() //輸出異常名稱,異常信息,異常出現(xiàn)的位置
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( "除數(shù)有誤" ); //獲得異常信息 System.out.println(e.getMessage()); //獲得異常信息,異常名稱 System.out.println(e.toString()); //輸出異常名稱,異常信息,異常出現(xiàn)的位置 e.printStackTrace(); } System.out.println( "over" ); /*byte[] arr=new byte[1024*1024*1000];*/ } } |
運(yùn)行結(jié)果:
除數(shù)有誤
/ 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
從運(yùn)行結(jié)果分析,其實(shí)jvm默認(rèn)異常處理機(jī)制就是在調(diào)用printStackTrace方法。
實(shí)例3:拋出異常的兩種處理方式
1.拋出給jvm虛擬機(jī)處理
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 /*有可能出現(xiàn)異常的地方拋出異常*/ { 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" ); } } |
運(yùn)行結(jié)果:
ExceptionDemo.java:15: 錯(cuò)誤: 未報(bào)告的異常錯(cuò)誤Exception; 必須對(duì)其進(jìn)行捕獲或聲明以
便拋出
int x=d.div(4,0);
^
1 個(gè)錯(cuò)誤
結(jié)果分析:這是因?yàn)闆](méi)有對(duì)有可能出現(xiàn)異常進(jìn)行處理
處理方式1:不斷拋出異常,讓jvm虛擬機(jī)自己處理
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 /*有可能出現(xiàn)異常的地方拋出異常*/ { return x/y; } } class ExceptionDemo { public static void main(String args[]) throws Exception /*繼續(xù)拋出異常,給虛擬機(jī)*/ { 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 /*有可能出現(xiàn)異常的地方拋出異常*/ { 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( "除數(shù)有誤" ); //獲得異常信息,異常名稱 System.out.println(e.toString()); System.out.println( "over" ); } } } |
總結(jié):
在函數(shù)上聲明異常。便于提高安全性,讓調(diào)出處進(jìn)行處理,不處理編譯失敗。
實(shí)例4:對(duì)多異常處理
1.聲明異常時(shí),建議聲明更為具體的異常,這樣處理得可以更具體
2.聲明幾個(gè)異常,就對(duì)應(yīng)有幾個(gè)catch塊,不要定義多余的catch快。
如果有多個(gè)catch塊中的異常出現(xiàn)繼承關(guān)系,父類異常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) /*除法法則異常對(duì)象接收,第一個(gè)執(zhí)行*/ { System.out.println("除數(shù)有誤"); //獲得異常信息,異常名稱 System.out.println(e.toString()); System.out.println("over"); } catch(ArrayIndexOutOfBoundsException e) /*數(shù)據(jù)越界的對(duì)象接收,第二個(gè)執(zhí)行*/ { System.out.println("數(shù)組越界了"); //輸出異常信息 System.out.println(e.toString()); } catch(Exception e) /*父類Exception接收,最后執(zhí)行,建議不要寫這個(gè),讓程序終止*/ /*用到了多態(tài)*/ { System.out.println(e.toString()); } } } |
運(yùn)行結(jié)果:
數(shù)組越界了
java.lang.ArrayIndexOutOfBoundsException: 4
建議:
建立在catch處理時(shí),catch中一定要定義具體的處理方式
不要簡(jiǎn)單定義一句 e.printStackTrace().
也不要簡(jiǎn)單就書寫一條輸出語(yǔ)句
因?yàn)橛脩艨床欢詈帽4娴轿募校〞r(shí)發(fā)給我們開(kāi)發(fā)者去查看。
實(shí)例5:自定義異常
你們有沒(méi)有發(fā)現(xiàn),我們正在使用的異常都是java中封裝好的
但在實(shí)際開(kāi)發(fā)中,我們的程序中出現(xiàn)的異常,有可能是java沒(méi)有封裝的,
這時(shí)候,就需要自己定義了
我根據(jù)上面的代碼,定義除數(shù)不能為負(fù)數(shù),代碼如下
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("分母出現(xiàn)負(fù)數(shù)了------/bu FuShu",y); /*自己手動(dòng)拋出異常的對(duì)象*/ } return x/y; } } class FuShuException extends Exception { private int value; FuShuException(String m,int value) { super(m); /*給父類Exception的getMessage方法傳遞參數(shù)*/ this.value=value; } public int getValue() /*自定義的方法,返回負(fù)數(shù)*/ { 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) /*捕獲異常對(duì)象*/ { System.out.println(e.getMessage()+e.getValue()); } System.out.println( "over" ); } } |
運(yùn)行結(jié)果:
分母出現(xiàn)負(fù)數(shù)了------/bu FuShu-3
over
從上面的結(jié)果,可以看出
在本程序中,對(duì)于除數(shù)是-3,也視為是錯(cuò)誤的是無(wú)法進(jìn)行運(yùn)算的。
那么就需要對(duì)這個(gè)問(wèn)題進(jìn)行自定義的描述。
當(dāng)在函數(shù)內(nèi)部出現(xiàn)了throw拋出異常對(duì)象,那么就必須要給對(duì)應(yīng)的處理動(dòng)作。
要么在內(nèi)部try catch處理。
要么在函數(shù)上聲明讓調(diào)用者處理。
一般情況在,函數(shù)內(nèi)出現(xiàn)異常,函數(shù)上需要聲明。
發(fā)現(xiàn)打印的結(jié)果中只有異常的名稱,卻沒(méi)有異常的信息。
因?yàn)樽远x的異常并未定義信息。
如何定義異常信息呢?
因?yàn)楦割愔幸呀?jīng)把異常信息的操作都完成了。
所以子類只要在構(gòu)造時(shí),將異常信息傳遞給父類通過(guò)super語(yǔ)句。
那么就可以直接通過(guò)getMessage方法獲取自定義的異常信息。
自定義異常必須是自定義類繼承Exception。
繼承Exception原因:
異常體系有一個(gè)特點(diǎn):因?yàn)楫惓n惡彤惓?duì)象都被拋出。
他們都具備可拋性。這個(gè)可拋性是Throwable這個(gè)體系中獨(dú)有特點(diǎn)。
只有這個(gè)體系中的類和對(duì)象才可以被throws和throw操作。
throws和throw的區(qū)別
throws使用在函數(shù)上。
throw使用在函數(shù)內(nèi)。
throws后面跟的異常類。可以跟多個(gè)。用逗號(hào)隔開(kāi)。
throw后跟的是異常對(duì)象。
實(shí)例6:Exception中有一個(gè)特殊的子類異常RuntimeException 運(yùn)行時(shí)異常
如果在函數(shù)內(nèi)容拋出該異常,函數(shù)上可以不聲明,編譯一樣通過(guò)。
如果函數(shù)上聲明了該異常,調(diào)用者可以不進(jìn)行處理,編譯一樣通過(guò)
之所以不用在函數(shù)聲明,是因?yàn)椴恍枰屨{(diào)用者處理
當(dāng)該異常發(fā)生,希望程序停止,因?yàn)樵谶\(yùn)行時(shí),出現(xiàn)了無(wú)法運(yùn)行的情況,希望程序停止后
程序員對(duì)該代碼進(jìn)行修改。
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 /*拋不拋結(jié)果都一樣*/ { if(y<0) { throw new FuShuException("分母出現(xiàn)負(fù)數(shù)了------/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); /*運(yùn)行到這會(huì)出現(xiàn)異常,編譯沒(méi)有問(wèn)題*/ System.out.println( "x=" +x); System.out.println( "over" ); } } |
運(yùn)行結(jié)果:
Exception in thread "main" FuShuException: 分母出現(xiàn)負(fù)數(shù)了------/bu FuShu
at Demo.div(ExceptionDemo.java:7)
at ExceptionDemo.main(ExceptionDemo.java:26)
從上面的結(jié)果可以看出:
自定義異常時(shí):如果該異常的發(fā)生,無(wú)法在繼續(xù)進(jìn)行運(yùn)算,
就讓自定義異常繼承RuntimeException。
對(duì)于異常分兩種:
1,編譯時(shí)被檢測(cè)的異常。
2,編譯時(shí)不被檢測(cè)的異常(運(yùn)行時(shí)異常。RuntimeException以及其子類)
以上這篇全面理解java中的異常處理機(jī)制就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。