- 相關(guān)推薦
全面理解java中的異常處理機制精選
一、java異?偨Y:
異常就是程序運行時(shí)出現不正常運行情況
1.異常由來(lái):
通過(guò)java的類(lèi)的形式對現實(shí)事物中問(wèn)題的描述,并封住成了對象
其實(shí)就是java對不正常情況描述后的對象體現
2.對于問(wèn)題的劃分有兩種:一種是嚴重的問(wèn)題,一種是非嚴重的問(wèn)題
對于嚴重的,java通過(guò)Error類(lèi)來(lái)描述
對于Error一般不編寫(xiě)針對性的代碼對其進(jìn)行處理
對于非嚴重的,java通過(guò)Exception類(lèi)來(lái)描述
對于Exception可以使用針對性的處理方式進(jìn)行處理
3.常見(jiàn)的異常有:數組角標越界異常,空指針異!
4.無(wú)論Error或者Exception都有一些共性的內容。
比如:不正常情況的消息,引發(fā)原因等。
Throwable //父類(lèi)(下面兩個(gè)類(lèi)相同的共性抽取出來(lái)的)
|--Error
|--Excption //兩個(gè)子類(lèi)(里面定義了很多問(wèn)題(異常出現)) /*父類(lèi)名作為子類(lèi)后綴名*/
實(shí)例1:出現異常示例
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,這時(shí)候運行就拋出了異常。
實(shí)例2:出現異常示例2,內存溢出
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提供了特有的語(yǔ)句進(jìn)行處理
格式
try
{
需要被檢測的代碼;
}
catch
{
處理異常的代碼;(處理方式)
}
finally
{
一定會(huì )執行的代碼;(處理方式)
}
實(shí)例1:演示try catch語(yǔ)句
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
結果分析:程序在運行時(shí),當執行到除法的語(yǔ)句:return x/y時(shí),就生成了異常的對象 new AritchmeticException(),try語(yǔ)句把這個(gè)對象讓catch語(yǔ)句的參數捕獲
Exception e =new AritchmeticException();
運行完catch的處理語(yǔ)句后,問(wèn)題就被處理完了,結束語(yǔ)句,輸出over
實(shí)例2:對捕獲到的異常對象進(jìn)行常見(jiàn)的方法操作(父類(lèi)Throwable的方法)
String getMessage(); //獲取異常信息
toString() //返回異常名稱(chēng):異常信息
printStackTrace() //輸出異常名稱(chēng),異常信息,異常出現的位置
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()); //獲得異常信息,異常名稱(chēng) System.out.println(e.toString()); //輸出異常名稱(chēng),異常信息,異常出現的位置 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
從運行結果分析,其實(shí)jvm默認異常處理機制就是在調用printStackTrace方法。
實(shí)例3:拋出異常的兩種處理方式
1.拋出給jvm虛擬機處理
2.拋出的異常自己處理
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; 必須對其進(jìn)行捕獲或聲明以
便拋出
int x=d.div(4,0);
^
1 個(gè)錯誤
結果分析:這是因為沒(méi)有對有可能出現異常進(jìn)行處理
處理方式1:不斷拋出異常,讓jvm虛擬機自己處理
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:自己處理異常
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("除數有誤"); //獲得異常信息,異常名稱(chēng) System.out.println(e.toString()); System.out.println("over"); } }}
總結:
在函數上聲明異常。便于提高安全性,讓調出處進(jìn)行處理,不處理編譯失敗。
實(shí)例4:對多異常處理
1.聲明異常時(shí),建議聲明更為具體的異常,這樣處理得可以更具體
2.聲明幾個(gè)異常,就對應有幾個(gè)catch塊,不要定義多余的catch快。
如果有多個(gè)catch塊中的異常出現繼承關(guān)系,父類(lèi)異常catch塊放在下面。
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) /*除法法則異常對象接收,第一個(gè)執行*/ { System.out.println("除數有誤"); //獲得異常信息,異常名稱(chēng) System.out.println(e.toString()); System.out.println("over"); } catch(ArrayIndexOutOfBoundsException e) /*數據越界的對象接收,第二個(gè)執行*/ { System.out.println("數組越界了"); //輸出異常信息 System.out.println(e.toString()); } catch(Exception e) /*父類(lèi)Exception接收,最后執行,建議不要寫(xiě)這個(gè),讓程序終止*/ /*用到了多態(tài)*/ { System.out.println(e.toString()); } }}
運行結果:
數組越界了
java.lang.ArrayIndexOutOfBoundsException: 4
建議:
建立在catch處理時(shí),catch中一定要定義具體的處理方式
不要簡(jiǎn)單定義一句 e.printStackTrace().
也不要簡(jiǎn)單就書(shū)寫(xiě)一條輸出語(yǔ)句
因為用戶(hù)看不懂,最好保存到文件中,定時(shí)發(fā)給我們開(kāi)發(fā)者去查看。
實(shí)例5:自定義異常
你們有沒(méi)有發(fā)現,我們正在使用的異常都是java中封裝好的
但在實(shí)際開(kāi)發(fā)中,我們的程序中出現的異常,有可能是java沒(méi)有封裝的,
這時(shí)候,就需要自己定義了
我根據上面的代碼,定義除數不能為負數,代碼如下
class Demo{ public int div(int x,int y)throws FuShuException /*拋出異常*/ { if(y<0) { throw new FuShuException("分母出現負數了------/bu FuShu",y); /*自己手動(dòng)拋出異常的對象*/ } return x/y; }}class FuShuException extends Exception{ private int value; FuShuException(String m,int value) { super(m); /*給父類(lèi)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,也視為是錯誤的是無(wú)法進(jìn)行運算的。
那么就需要對這個(gè)問(wèn)題進(jìn)行自定義的描述。
當在函數內部出現了throw拋出異常對象,那么就必須要給對應的處理動(dòng)作。
要么在內部try catch處理。
要么在函數上聲明讓調用者處理。
一般情況在,函數內出現異常,函數上需要聲明。
發(fā)現打印的結果中只有異常的名稱(chēng),卻沒(méi)有異常的信息。
因為自定義的異常并未定義信息。
如何定義異常信息呢?
因為父類(lèi)中已經(jīng)把異常信息的操作都完成了。
所以子類(lèi)只要在構造時(shí),將異常信息傳遞給父類(lèi)通過(guò)super語(yǔ)句。
那么就可以直接通過(guò)getMessage方法獲取自定義的異常信息。
自定義異常必須是自定義類(lèi)繼承Exception。
繼承Exception原因:
異常體系有一個(gè)特點(diǎn):因為異常類(lèi)和異常對象都被拋出。
他們都具備可拋性。這個(gè)可拋性是Throwable這個(gè)體系中獨有特點(diǎn)。
只有這個(gè)體系中的類(lèi)和對象才可以被throws和throw操作。
throws和throw的區別
throws使用在函數上。
throw使用在函數內。
throws后面跟的異常類(lèi)?梢愿鄠(gè)。用逗號隔開(kāi)。
throw后跟的是異常對象。
實(shí)例6:Exception中有一個(gè)特殊的子類(lèi)異常RuntimeException 運行時(shí)異常
如果在函數內容拋出該異常,函數上可以不聲明,編譯一樣通過(guò)。
如果函數上聲明了該異常,調用者可以不進(jìn)行處理,編譯一樣通過(guò)
之所以不用在函數聲明,是因為不需要讓調用者處理
當該異常發(fā)生,希望程序停止,因為在運行時(shí),出現了無(wú)法運行的情況,希望程序停止后
程序員對該代碼進(jìn)行修改。
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); /*運行到這會(huì )出現異常,編譯沒(méi)有問(wèn)題*/ 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)
從上面的結果可以看出:
自定義異常時(shí):如果該異常的發(fā)生,無(wú)法在繼續進(jìn)行運算,
就讓自定義異常繼承RuntimeException。
對于異常分兩種:
1,編譯時(shí)被檢測的異常。
2,編譯時(shí)不被檢測的異常(運行時(shí)異常。RuntimeException以及其子類(lèi))
以上這篇全面理解java中的異常處理機制就是小編分享給大家的全部?jì)热萘,希望能給大家一個(gè)參考,也希望大家多多支持。
【全面理解java中的異常處理機制】相關(guān)文章:
Java的異常機制分析及處理辦法02-05
Java語(yǔ)言中的異常處理機制08-02
Java編程中異常處理的方法06-03
Java 異常處理12-22
Java編程中異常處理的最優(yōu)法08-06
Java的異常處理及應用03-12
Java異常處理語(yǔ)句及解析07-27
java中反射機制05-26
PHP7系列中的異常處理08-11