更新時(shí)間:2019-09-11 11:54:07 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2384次
動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)小編分享的“JAVA中異常與異常處理詳解”的內(nèi)容太長(zhǎng),請(qǐng)看上文:http://www.dabaquan.cn/javazixun/1828.html
Java自定義異常
如果要自定義異常類(lèi),則擴(kuò)展Exception類(lèi)即可,因此這樣的自定義異常都屬于檢查異常(checked exception)。如果要自定義非檢查異常,則擴(kuò)展自RuntimeException。
按照國(guó)際慣例,自定義的異常應(yīng)該總是包含如下的構(gòu)造函數(shù):
一個(gè)無(wú)參構(gòu)造函數(shù)
一個(gè)帶有String參數(shù)的構(gòu)造函數(shù),并傳遞給父類(lèi)的構(gòu)造函數(shù)。
一個(gè)帶有String參數(shù)和Throwable參數(shù),并都傳遞給父類(lèi)構(gòu)造函數(shù)
一個(gè)帶有Throwable 參數(shù)的構(gòu)造函數(shù),并傳遞給父類(lèi)的構(gòu)造函數(shù)。
下面是IOException類(lèi)的完整源代碼:
public class IOException extends Exception
{
static final long serialVersionUID = 7818375828146090155L;
public IOException()
{
super();
}
public IOException(String message)
{
super(message);
}
public IOException(String message, Throwable cause)
{
super(message, cause);
}
public IOException(Throwable cause)
{
super(cause);
}
}
Java異常的注意事項(xiàng)
1、當(dāng)子類(lèi)重寫(xiě)父類(lèi)的帶有 throws聲明的函數(shù)時(shí),其throws聲明的異常必須在父類(lèi)異常的可控范圍內(nèi)——用于處理父類(lèi)的throws方法的異常處理器,必須也適用于子類(lèi)的這個(gè)帶throws方法 。這是為了支持多態(tài)。
例如,父類(lèi)方法throws 的是2個(gè)異常,子類(lèi)就不能throws 3個(gè)及以上的異常。父類(lèi)throws IOException,子類(lèi)就必須throws IOException或者IOException的子類(lèi)。
示例:
class Father
{
public void start() throws IOException
{
throw new IOException();
}
}
class Son extends Father
{
public void start() throws Exception
{
throw new SQLException();
}
}
/**********************假設(shè)上面的代碼是允許的(實(shí)質(zhì)是錯(cuò)誤的)***********************/
class Test
{
public static void main(String[] args)
{
Father[] objs = new Father[2];
objs[0] = new Father();
objs[1] = new Son();
for(Father obj:objs)
{
//因?yàn)镾on類(lèi)拋出的實(shí)質(zhì)是SQLException,而IOException無(wú)法處理它。
//那么這里的try。。catch就不能處理Son中的異常。
//多態(tài)就不能實(shí)現(xiàn)了。
try {
obj.start();
}catch(IOException)
{
//處理IOException
}
}
}
}
2、Java程序可以是多線程的。每一個(gè)線程都是一個(gè)獨(dú)立的執(zhí)行流,獨(dú)立的函數(shù)調(diào)用棧。如果程序只有一個(gè)線程,那么沒(méi)有被任何代碼處理的異常 會(huì)導(dǎo)致程序終止。如果是多線程的,那么沒(méi)有被任何代碼處理的異常僅僅會(huì)導(dǎo)致異常所在的線程結(jié)束。
也就是說(shuō),Java中的異常是線程獨(dú)立的,線程的問(wèn)題應(yīng)該由線程自己來(lái)解決,而不要委托到外部,也不會(huì)直接影響到其它線程的執(zhí)行。
finally塊和return
首先一個(gè)不容易理解的事實(shí):在 try塊中即便有return,break,continue等改變執(zhí)行流的語(yǔ)句,finally也會(huì)執(zhí)行。
public static void main(String[] args)
{
int re = bar();
System.out.println(re);
}
private static int bar()
{
try{
return 5;
} finally{
System.out.println("finally");
}
}
/*輸出:
finally
*/
很多人面對(duì)這個(gè)問(wèn)題時(shí),總是在歸納執(zhí)行的順序和規(guī)律,小編總結(jié)了一個(gè)方法。用如下GIF圖說(shuō)明。
也就是說(shuō):try...catch...finally中的return 只要能執(zhí)行,就都執(zhí)行了,他們共同向同一個(gè)內(nèi)存地址(假設(shè)地址是0x80)寫(xiě)入返回值,后執(zhí)行的將覆蓋先執(zhí)行的數(shù)據(jù),而真正被調(diào)用者取的返回值就是最后一次寫(xiě)入的。那么,按照這個(gè)思想,下面的這個(gè)例子也就不難理解了。
finally中的return 會(huì)覆蓋 try 或者catch中的返回值。
public static void main(String[] args)
{
int result;
result = foo();
System.out.println(result); /////////2
result = bar();
System.out.println(result); /////////2
}
@SuppressWarnings("finally")
public static int foo()
{
trz{
int a = 5 / 0;
} catch (Exception e){
return 1;
} finally{
return 2;
}
}
@SuppressWarnings("finally")
public static int bar()
{
try {
return 1;
}finally {
return 2;
}
}
finally中的return會(huì)抑制(消滅)前面try或者catch塊中的異常
class TestException
{
public static void main(String[] args)
{
int result;
try{
result = foo();
System.out.println(result); //輸出100
} catch (Exception e){
System.out.println(e.getMessage()); //沒(méi)有捕獲到異常
}
try{
result = bar();
System.out.println(result); //輸出100
} catch (Exception e){
System.out.println(e.getMessage()); //沒(méi)有捕獲到異常
}
}
//catch中的異常被抑制
@SuppressWarnings("finally")
public static int foo() throws Exception
{
try {
int a = 5/0;
return 1;
}catch(ArithmeticException amExp) {
throw new Exception("我將被忽略,因?yàn)橄旅娴膄inally中使用了return");
}finally {
return 100;
}
}
//try中的異常被抑制
@SuppressWarnings("finally")
public static int bar() throws Exception
{
try {
int a = 5/0;
return 1;
}finally {
return 100;
}
}
}
finally中的異常會(huì)覆蓋(消滅)前面try或者catch中的異常
class TestException
{
public static void main(String[] args)
{
int result;
try{
result = foo();
} catch (Exception e){
System.out.println(e.getMessage()); //輸出:我是finaly中的Exception
}
try{
result = bar();
} catch (Exception e){
System.out.println(e.getMessage()); //輸出:我是finaly中的Exception
}
}
//catch中的異常被抑制
@SuppressWarnings("finally")
public static int foo() throws Exception
{
try {
int a = 5/0;
return 1;
}catch(ArithmeticException amExp) {
throw new Exception("我將被忽略,因?yàn)橄旅娴膄inally中拋出了新的異常");
}finally {
throw new Exception("我是finaly中的Exception");
}
}
//try中的異常被抑制
@SuppressWarnings("finally")
public static int bar() throws Exception
{
try {
int a = 5/0;
return 1;
}finally {
throw new Exception("我是finaly中的Exception");
}
}
}
注意:
不要在fianlly中使用return。
不要在finally中拋出異常。
減輕finally的任務(wù),不要在finally中做一些其它的事情,finally塊僅僅用來(lái)釋放資源是最合適的。
將盡量將所有的return寫(xiě)在函數(shù)的最后面,而不是try ... catch ... finally中。
以上就是動(dòng)力節(jié)點(diǎn)Java培訓(xùn)機(jī)構(gòu)小編介紹的“JAVA中異常與異常處理詳解”的內(nèi)容,希望對(duì)大家有幫助,更多Java最新資訊請(qǐng)繼續(xù)關(guān)注動(dòng)力節(jié)點(diǎn)Java培訓(xùn)機(jī)構(gòu)官網(wǎng),每天會(huì)有精彩內(nèi)容分享與你。
相關(guān)視頻教程推薦
java初級(jí)入門(mén)教程下載——Java自定義異常:http://www.dabaquan.cn/xiazai/1023.html
java初級(jí)入門(mén)教程下載——異常捕獲預(yù)處理:http://www.dabaquan.cn/xiazai/2555.html
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743