全面理解java中的異常處理機(jī)制
一、java異??偨Y(jié):
異常就是程序運(yùn)行時(shí)出現(xiàn)不正常運(yùn)行情況
1.異常由來:
通過java的類的形式對(duì)現(xiàn)實(shí)事物中問題的描述,并封住成了對(duì)象
其實(shí)就是java對(duì)不正常情況描述后的對(duì)象體現(xiàn)
2.對(duì)于問題的劃分有兩種:一種是嚴(yán)重的問題,一種是非嚴(yán)重的問題
對(duì)于嚴(yán)重的,java通過Error類來描述
對(duì)于Error一般不編寫針對(duì)性的代碼對(duì)其進(jìn)行處理
對(duì)于非嚴(yán)重的,java通過Exception類來描述
對(duì)于Exception可以使用針對(duì)性的處理方式進(jìn)行處理
3.常見的異常有:數(shù)組角標(biāo)越界異常,空指針異?!?/strong>
4.無論Error或者Exception都有一些共性的內(nèi)容。
比如:不正常情況的消息,引發(fā)原因等。
Throwable //父類(下面兩個(gè)類相同的共性抽取出來的)
|--Error
|--Excption //兩個(gè)子類(里面定義了很多問題(異常出現(xiàn))) /*父類名作為子類后綴名*/
實(shí)例1:出現(xiàn)異常示例
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)存溢出
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提供了特有的語句進(jìn)行處理
格式
try
{
需要被檢測(cè)的代碼;
}
catch
{
處理異常的代碼;(處理方式)
}
finally
{
一定會(huì)執(zhí)行的代碼;(處理方式)
}
實(shí)例1:演示try catch語句
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í)行到除法的語句:return x/y時(shí),就生成了異常的對(duì)象 new AritchmeticException(),try語句把這個(gè)對(duì)象讓catch語句的參數(shù)捕獲
Exception e =new AritchmeticException();
運(yùn)行完catch的處理語句后,問題就被處理完了,結(jié)束語句,輸出over
實(shí)例2:對(duì)捕獲到的異常對(duì)象進(jìn)行常見的方法操作(父類Throwable的方法)
String getMessage(); //獲取異常信息
toString() //返回異常名稱:異常信息
printStackTrace() //輸出異常名稱,異常信息,異常出現(xiàn)的位置
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.拋出的異常自己處理
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)闆]有對(duì)有可能出現(xiàn)異常進(jìn)行處理
處理方式1:不斷拋出異常,讓jvm虛擬機(jī)自己處理
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:自己處理異常
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塊放在下面。
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àn)橛脩艨床欢?,最好保存到文件中,定時(shí)發(fā)給我們開發(fā)者去查看。
實(shí)例5:自定義異常
你們有沒有發(fā)現(xiàn),我們正在使用的異常都是java中封裝好的
但在實(shí)際開發(fā)中,我們的程序中出現(xiàn)的異常,有可能是java沒有封裝的,
這時(shí)候,就需要自己定義了
我根據(jù)上面的代碼,定義除數(shù)不能為負(fù)數(shù),代碼如下
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ò)誤的是無法進(jìn)行運(yùn)算的。
那么就需要對(duì)這個(gè)問題進(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é)果中只有異常的名稱,卻沒有異常的信息。
因?yàn)樽远x的異常并未定義信息。
如何定義異常信息呢?
因?yàn)楦割愔幸呀?jīng)把異常信息的操作都完成了。
所以子類只要在構(gòu)造時(shí),將異常信息傳遞給父類通過super語句。
那么就可以直接通過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)隔開。
throw后跟的是異常對(duì)象。
實(shí)例6:Exception中有一個(gè)特殊的子類異常RuntimeException 運(yùn)行時(shí)異常
如果在函數(shù)內(nèi)容拋出該異常,函數(shù)上可以不聲明,編譯一樣通過。
如果函數(shù)上聲明了該異常,調(diào)用者可以不進(jìn)行處理,編譯一樣通過
之所以不用在函數(shù)聲明,是因?yàn)椴恍枰屨{(diào)用者處理
當(dāng)該異常發(fā)生,希望程序停止,因?yàn)樵谶\(yùn)行時(shí),出現(xiàn)了無法運(yùn)行的情況,希望程序停止后
程序員對(duì)該代碼進(jìn)行修改。
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)異常,編譯沒有問題*/
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ā)生,無法在繼續(xù)進(jìn)行運(yùn)算,
就讓自定義異常繼承RuntimeException。
對(duì)于異常分兩種:
1,編譯時(shí)被檢測(cè)的異常。
2,編譯時(shí)不被檢測(cè)的異常(運(yùn)行時(shí)異常。RuntimeException以及其子類)
以上這篇全面理解java中的異常處理機(jī)制就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?MVC概念+項(xiàng)目創(chuàng)建+@RequestMappring案例代碼
Spring?MVC?是?Spring?提供的一個(gè)基于?MVC?設(shè)計(jì)模式的輕量級(jí)?Web?開發(fā)框架,本質(zhì)上相當(dāng)于?Servlet,這篇文章主要介紹了Spring?MVC概念+項(xiàng)目創(chuàng)建+@RequestMappring,需要的朋友可以參考下2023-02-02
Java中的while無限循環(huán)結(jié)構(gòu)及實(shí)例
這篇文章主要介紹了Java中的while無限循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot項(xiàng)目部署時(shí)application.yml文件的加載優(yōu)先級(jí)和啟動(dòng)腳本問題
Spring Boot在啟動(dòng)時(shí)會(huì)根據(jù)一定的優(yōu)先級(jí)順序加載配置文件,優(yōu)先級(jí)從高到低依次是:命令行參數(shù)、Jar包外部config目錄下的配置文件、Jar包同級(jí)目錄下的配置文件、classpath下的/config目錄、classpath根路徑2024-09-09
java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Java統(tǒng)計(jì)字符串中特定字符出現(xiàn)次數(shù)的幾種常用方法
在編程的過程中,我們常常會(huì)遇到需要分析字符串的情況,比如統(tǒng)計(jì)某個(gè)特定字符出現(xiàn)的次數(shù),這種操作在處理文本數(shù)據(jù)時(shí)非常實(shí)用,今天,我們就來聊一聊如何在Java中統(tǒng)計(jì)字符串中特定字符的出現(xiàn)次數(shù),需要的朋友可以參考下2025-04-04
Java基于swing實(shí)現(xiàn)的彈球游戲代碼
這篇文章主要介紹了Java基于swing實(shí)現(xiàn)的彈球游戲代碼,包含了窗體界面設(shè)計(jì)與游戲的邏輯功能處理,具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11

