Java牛角尖【007】:Java中的Error能不能被Catch

來源:互聯網
上載者:User

標籤:under   view   很多   tail   com   執行   r.java   xtend   blank   

  Java牛角尖【007】:Java中的Error能不能被Catch

網上看到很多朋友說Java中Error是無法Catch到的,而Java中定義的Error類型又很難測試到,那就估且以為確是如此吧。

但是或許大家都有注意,我們時常會看到這樣的代碼

 

 

[java] view plain copy 
  1. try{  
  2.     ...  
  3. }catch(Throwable ex){  
  4.     ...  
  5. }  

 

 

其中catch中直接捕捉的是一個Throwable類,開啟繼承關係看一下,Exception和Error兩個類同樣是從Throwable類繼承而來,那麼,也就是說Error應該是可以被捕捉的,下面寫個例子證明一下猜測:

 

 

[java] view plain copy 
  1. package net.moon.demo.errorcatch;  
  2.   
  3. public class Demo {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.         try {  
  11.             throw new MyError("My Error");  
  12.         } catch (MyError e) {  
  13.             System.out.println(e.getMessage());  
  14.         }  
  15.     }  
  16.   
  17. }  
  18.   
  19. class MyError extends Error {  
  20.   
  21.     /** 
  22.      *  
  23.      */  
  24.     private static final long serialVersionUID = 1L;  
  25.   
  26.     public MyError() {  
  27.         super();  
  28.         // TODO Auto-generated constructor stub  
  29.     }  
  30.   
  31.     public MyError(String message, Throwable cause) {  
  32.         super(message, cause);  
  33.         // TODO Auto-generated constructor stub  
  34.     }  
  35.   
  36.     public MyError(String message) {  
  37.         super(message);  
  38.         // TODO Auto-generated constructor stub  
  39.     }  
  40.   
  41.     public MyError(Throwable cause) {  
  42.         super(cause);  
  43.         // TODO Auto-generated constructor stub  
  44.     }  
  45.   
  46. }  

 

 

執行一下以上代碼,正如前面的猜測,Error一樣是可以捕捉的,運行代碼結果為:

 

 

[xhtml] view plain copy 
  1. My Error  

 

 

 

下面給個小例子,來驗證一下error的捕獲。

 

複製代碼代碼如下:
public class TestCatchError extends Error{

    private static final long serialVersionUID = -351488225420878020L;

 

    public TestCatchError(){
        super();
    }

    public TestCatchError(String msg){
        super(msg);
    }

    public static void main(String[] args) {
        try {
            throw new TestCatchError("test catch error");
        } catch (Throwable t) {
            System.out.println("step in the catch ~");
            t.printStackTrace();
        }
    }
}

 

運行結果:

複製代碼代碼如下:
step in the catch ~
TestCatchError: test catch error
at TestCatchError.main(TestCatchError.java:23)

 

Java牛角尖【007】:Java中的Error能不能被Catch

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.