Java提供了兩類主要的異常:runtime exception和checked exception。所有的checked exception是從java.lang.Exception類衍生出來的,而runtime exception則是從java.lang.RuntimeException或java.lang.Error類衍生出來的。
它們的不同之處表現(xiàn)在兩方面:機(jī)制上和邏輯上。
一、機(jī)制上
它們在機(jī)制上的不同表現(xiàn)在兩點(diǎn):1.如何定義方法;2. 如何處理拋出的異常。請看下面CheckedException的定義:
public class CheckedException extends Exception { public CheckedException() {} public CheckedException( String message ) { super( message ); } } |
以及一個使用exception的例子:
public class ExceptionalClass { public void method1() throws CheckedException { // ... throw new CheckedException( "...出錯了" ); } public void method2( String arg ) { if( arg == null ) { throw new NullPointerException( "method2的參數(shù)arg是null!" ); } } public void method3() throws CheckedException { method1(); } } |
你可能已經(jīng)注意到了,兩個方法method1()和method2()都會拋出exception,可是只有method1()做了聲明。另外,method3()本身并不會拋出exception,可是它卻聲明會拋出CheckedException。在向你解釋之前,讓我們先來看看這個類的main()方法:
public static void main( String[] args ) { ExceptionalClass example = new ExceptionalClass(); try { example.method1(); example.method3(); } catch( CheckedException ex ) { } example.method2( null ); } |
在main()方法中,如果要調(diào)用method1(),你必須把這個調(diào)用放在try/catch程序塊當(dāng)中,因?yàn)樗鼤伋鯟hecked exception。
相比之下,當(dāng)你調(diào)用method2()時,則不需要把它放在try/catch程序塊當(dāng)中,因?yàn)樗鼤伋龅膃xception不是checked exception,而是runtime exception。會拋出runtime exception的方法在定義時不必聲明它會拋出exception。
現(xiàn)在,讓我們再來看看method3()。它調(diào)用了method1()卻沒有把這個調(diào)用放在try/catch程序塊當(dāng)中。它是通過聲明它會拋出method1()會拋出的exception來避免這樣做的。它沒有捕獲這個exception,而是把它傳遞下去。實(shí)際上main()方法也可以這樣做,通過聲明它會拋出Checked exception來避免使用try/catch程序塊(當(dāng)然我們反對這種做法)。
小結(jié)一下:
* Runtime exceptions:
在定義方法時不需要聲明會拋出runtime exception;
在調(diào)用這個方法時不需要捕獲這個runtime exception;
runtime exception是從java.lang.RuntimeException或java.lang.Error類衍生出來的。
* Checked exceptions:
定義方法時必須聲明所有可能會拋出的checked exception;
在調(diào)用這個方法時,必須捕獲它的checked exception,不然就得把它的exception傳遞下去;
checked exception是從java.lang.Exception類衍生出來的。
二、邏輯上
從邏輯的角度來說,checked exceptions和runtime exception是有不同的使用目的的。checked exception用來指示一種調(diào)用方能夠直接處理的異常情況。而runtime exception則用來指示一種調(diào)用方本身無法處理或恢復(fù)的程序錯誤。
checked exception迫使你捕獲它并處理這種異常情況。以java.net.URL類的構(gòu)建器(constructor)為例,它的每一個構(gòu)建器都會拋出MalformedURLException。MalformedURLException就是一種checked exception。設(shè)想一下,你有一個簡單的程序,用來提示用戶輸入一個URL,然后通過這個URL去下載一個網(wǎng)頁。如果用戶輸入的URL有錯誤,構(gòu)建器就會拋出一個exception。既然這個exception是checked exception,你的程序就可以捕獲它并正確處理:比如說提示用戶重新輸入。
再看下面這個例子:
public void method() { int [] numbers = { 1, 2, 3 }; int sum = numbers[0] + numbers[3]; } |
在運(yùn)行方法method()時會遇到ArrayIndexOutOfBoundsException(因?yàn)閿?shù)組numbers的成員是從0到2)。對于這個異常,調(diào)用方無法處理/糾正。這個方法method()和上面的method2()一樣,都是runtime exception的情形。上面我已經(jīng)提到,runtime exception用來指示一種調(diào)用方本身無法處理/恢復(fù)的程序錯誤。而程序錯誤通常是無法在運(yùn)行過程中處理的,必須改正程序代碼。
總而言之,在程序的運(yùn)行過程中一個checked exception被拋出的時候,只有能夠適當(dāng)處理這個異常的調(diào)用方才應(yīng)該用try/catch來捕獲它。而對于runtime exception,則不應(yīng)當(dāng)在程序中捕獲它。如果你要捕獲它的話,你就會冒這樣一個風(fēng)險:程序代碼的錯誤(bug)被掩蓋在運(yùn)行當(dāng)中無法被察覺。因?yàn)樵诔绦驕y試過程中,系統(tǒng)打印出來的調(diào)用堆棧路徑(StackTrace)往往使你更快找到并修改代碼中的錯誤。有些程序員建議捕獲runtime exception并紀(jì)錄在log中,我反對這樣做。這樣做的壞處是你必須通過瀏覽log來找出問題,而用來測試程序的測試系統(tǒng)(比如Unit Test)卻無法直接捕獲問題并報告出來。
在程序中捕獲runtime exception還會帶來更多的問題:要捕獲哪些runtime exception?什么時候捕獲?runtime exception是不需要聲明的,你怎樣知道有沒有runtime exception要捕獲?你想看到在程序中每一次調(diào)用方法時,都使用try/catch程序塊嗎?
--------------附加程序-------------------------
public class Junk {
// public static void main(String args[]) {
// try {
// a();
// } catch(HighLevelException e) {
// e.printStackTrace();
// }
// }
public static void main(String args[]) {
try {
a();
} catch(Exception e) {
e.printStackTrace();
System.out.println("-------------------------");
System.out.println(e.getMessage());
}
}
static void a() throws HighLevelException {
try {
b();
} catch(MidLevelException e) {
throw new HighLevelException(e);
}
}
static void b() throws MidLevelException {
c();
}
static void c() throws MidLevelException {
try {
d();
} catch(LowLevelException e) {
throw new MidLevelException(e);
}
}
static void d() throws LowLevelException {
e();
}
static void e() throws LowLevelException {
throw new LowLevelException("e throw exception!");
}
}
class HighLevelException extends Exception {
HighLevelException(Throwable cause) { super(cause); }
HighLevelException(String message,Throwable cause) {
super(message,cause);
}
}
class MidLevelException extends Exception {
MidLevelException(Throwable cause) { super(cause); }
MidLevelException(String message,Throwable cause) {
super(message,cause);
}
}
class LowLevelException extends Exception {
public LowLevelException(){
}
public LowLevelException(String message){
super(message);
}
}