Throwable、Error、Exception、RuntimeException 區別 聯繫

1.Throwable 類是 Java 語言中全部錯誤或異常的超類。它的兩個子類是Error和Exception;


2.Error 是 Throwable 的子類,用於指示合理的應用程序不該該試圖捕獲的嚴重問題。大多數這樣的錯誤都是異常條件。雖然 ThreadDeath 錯誤是一個「正規」的條件,但它也是 Error 的子類,由於大多數應用程序都不該該試圖捕獲它。在執行該方法期間,無需在其 throws 子句中聲明可能拋出可是未能捕獲的 Error 的任何子類,由於這些錯誤多是不再會發生的異常條件。


3.Exception 類及其子類是 Throwable 的一種形式,它指出了合理的應用程序想要捕獲的條件。


4.RuntimeException 是那些可能在 Java 虛擬機正常運行期間拋出的異常的超類。可能在執行方法期間拋出但未被捕獲的RuntimeException 的任何子類都無需在 throws 子句中進行聲明。它是Exception的子類。


5.方法重寫時:在子類中一個重寫的方法可能只拋出父類中聲明過的異常或者異常的子類 html


Error類和Exception類都繼承自Throwable類。 java

  • Error的繼承關係:

java.lang.Object 
 java.lang.Throwable
      java.lang.Error 程序員

 

  • Exception的繼承關係:

java.lang.Object
java.lang.Throwable
     java.lang.Exception 工具

 

 

兩者的不一樣之處: 測試

 

Exception:

1.能夠是可被控制(checked) 或不可控制的(unchecked) 

2.表示一個由程序員致使的錯誤 

3.應該在應用程序級被處理 spa

 

Error:

1.老是不可控制的(unchecked) 

2.常常用來用於表示系統錯誤或低層資源的錯誤 

3.如何可能的話,應該在系統級被捕捉 .net

 

 

 

Java 中定義了兩類異常:  設計


  1) Checked exception: 這類異常都是Exception的子類 。異常的向上拋出機制進行處理,假如子類可能產生A異常,那麼在父類中也必須throws A異常。可能致使的問題:代碼效率低,耦合度太高。
  
  2) Unchecked exception: 這類異常都是RuntimeException的子類,雖然RuntimeException一樣也是Exception的子類,可是它們是非凡的,它們不能經過client code來試圖解決,因此稱爲Unchecked exception 。 code

 

 

Java 中異常類的繼承關係圖: orm



 




Checked Exception與Runtime Exception 的區別

Java裏有個很重要的特點是Exception ,也就是說容許程序產生例外情況。而在學Java 的時候,咱們也只知道Exception 的寫法,卻未必真能瞭解不一樣種類的Exception 的區別。

  首先,您應該知道的是Java 提供了兩種Exception 的模式,一種是執行的時候所產生的Exception (Runtime Exception),另一種則是受控制的Exception (Checked Exception)。

  全部的Checked Exception 均從java.lang.Exception 繼承而來,而Runtime Exception 則繼承java.lang.RuntimeException 或java.lang.Error (實際上java.lang.RuntimeException 的上一層也是java.lang.Exception)。

  當咱們撰寫程序的時候,咱們極可能會對選擇某種形式的Exception 感到困擾,到底我應該選擇Runtime Exception 仍是Checked Exception ?

  其實,在運做上,咱們能夠經過Class 的Method 如何產生某個Exception以及某個程序如何處理這個被產生來的Exception 來了解它們之間的差別。
首先咱們先創建一個Exception

[java]  view plain copy
  1. public class CException extends Exception {  
  2.     public CException() {  
  3.     }  
  4.   
  5.     public CException(String message) {  
  6.         super(message);  
  7.     }  
  8. }  

而後咱們撰寫一個可能產生 CException 的 Class
[java]  view plain copy
  1. public class TestException {  
  2.     public void method1() throws CException {  
  3.         throw new CException("Test Exception");  
  4.     }  
  5.   
  6.     public void method2(String msg) {  
  7.         if (msg == null) {  
  8.             throw new NullPointerException("Message is null");  
  9.         }  
  10.     }  
  11.   
  12.     public void method3() throws CException {  
  13.         method1();  
  14.     }  
  15.   
  16.     // 如下省略  
  17.     // ...  
  18. }  
 在這三個method 中,咱們看到了method1 和method2 的程序碼內都會產生Exception,但method3 的程序碼中(大括號內),並沒產生Exception,但在method3 的定義中,暗示了這個method 可能產生CException。

  呼叫method1() 的程序,必須將method1() 包含在try 與catch 中,如:

[java]  view plain copy
  1. public class Runtest {  
  2.     // ....  
  3.     public static void main(String argv[]) {  
  4.         TestException te = new TestException();  
  5.         try {  
  6.             te.method1();  
  7.         } catch (CException ce) {  
  8.             // ....  
  9.             ce.printStackTrace();  
  10.         }  
  11.     }  
  12.     // ...  
  13. }  
雖然包含在try 與catch 中,並不表示這段程序碼必定會收到CException,但它的用意在於提醒呼叫者,執行這個method 可能產生的意外,而使用者也必需要能針對這個意外作出相對應的處理方式。

  當使用者呼叫method2() 時,並不須要使用try 和catch 將程序碼包起來,由於method2 的定義中,並無throws 任何的Exception ,如:

[java]  view plain copy
  1. public class Runtest  
  2. {  
  3. // ....  
  4. public static void main(String argv[])  
  5. {  
  6.   
  7. testException te = new testException();  
  8.   
  9. // 不會產生 Exception  
  10. te.method2("Hello");  
  11.   
  12. // 會產生 Exception  
  13. te.method2(null);  
  14. }  
  15. // ...  
  16. }  
程序在執行的時候,也不見得會真的產生NullPointerException ,這種Exception 叫作runtime exception 也有人稱爲unchecked exception ,產生Runtime Exception 的method (在這個範例中是method2) 並不須要在宣告method 的時候定義它將會產生哪種Exception 。

  在testException 的method3() 中,咱們看到了另一種情況,也就是method3裏呼叫了method1() ,但卻沒有將method1 包在try 和catch 之間。相反,在method3() 的定義中,它定義了CException,實際上就是若是method3 收到了CException ,它將不處理這個CException ,而將它往外丟。固然,因爲method3 的定義中有throws CException ,所以呼叫method3 的程序碼也須要有try catch 才行。

  所以從程序的運做機制上看,Runtime Exception與Checked Exception 不同,然而從邏輯上看,Runtime Exception 與Checked Exception 在使用的目的上也不同。

  通常而言,Checked Exception 表示這個Exception 必需要被處理,也就是說程序設計者應該已經知道可能會收到某個Exception(由於要try catch住) ,因此程序設計者應該能針對這些不一樣的Checked Exception 作出不一樣的處理。

  而Runtime Exception 一般會暗示着程序上的錯誤,這種錯誤會致使程序設計者沒法處理,而形成程序沒法繼續執行下去。

看看下面的例子:

String message[] = {"message1", "message2","message3"};
System.out.println(message[3]);

  這段程序碼在Compile 時並沒問題,但在執行時則會出現ArrayIndexOutOfBoundException 的例外,在這種情況下,咱們亦沒法針對這個Runtime Exception 作出有意義的動做,這就像是咱們呼叫了testException 中的method2 ,卻引起了它的NullPointerException 同樣,在這種情況下,咱們必須對程序碼進行修改,從而避免這個問題。

  所以,實際上咱們應該也必需要去抓取全部的Checked Exception,同時最好能在這些Checked Exception 發生的時候作出相對應的處理,好讓程序能面對不一樣的情況。

  然而對於Runtime Exception ,有些人建議將它catch 住,而後導向其它地方,讓程序繼續執行下去,這種做法並不是很差,但它會讓咱們在某些測試工具下認爲咱們的程序碼沒有問題,由於咱們將Runtime Exception "處理"掉了,事實卻否則!譬如不少人的習慣是在程序的進入點後用個大大的try catch 包起來,如:


[html]  view plain copy
  1. public class Runtest1 {  
  2.     public static void main(String argv[]) {  
  3.         try {  
  4.             // ...  
  5.         } catch (Exception e) {  
  6.         }  
  7.     }  
  8. }  

在這種狀況下,咱們極可能會不知道發生了什麼Exception 或是從哪一行發出的,所以在面對不一樣的Checked Exception時,咱們可已分別去try catch它。而在測試階段時,若是碰到Runtime Exception ,咱們可讓它就這樣發生,接着再去修改咱們的程序碼,讓它避免Runtime Exception,不然,咱們就應該仔細追究每個Exception ,直到咱們能夠肯定它不會有Runtime Exception 爲止!

  對於Checked Exception 與Runtime Exception ,我想應該有很多人會有不一樣的觀點,不管如何,程序先要能執行,這些Exception 纔有機會產生。所以,咱們能夠把這些Exception 當成是Bug ,也能夠當成是不一樣的情況(Checked Exception),或當成是幫助咱們除錯的工具(Runtime Exception),但前提是咱們須要處理這些Exception ,若是不處理,那麼問題或情況就會永遠留在那裏。

參考至:http://blog.csdn.net/liuj2511981/article/details/8524418
相關文章
相關標籤/搜索