2014-12-03 17 views
1

我讀有效的Java異常的文章現在創建Java異常做的事情不僅僅是打印留言

http://www.oracle.com/technetwork/java/effective-exceptions-092345.html

我在文章

不要的第三頁本款忘記你的例外是完整的Java類型,它們可以容納專門的領域,方法,甚至構造函數,這些專業領域,方法,甚至構造函數都可以爲你的獨特目的而塑造。例如,由假想 的CheckingAccount processCheck()方法拋出的 InsufficientFundsException類型可以包括一個 OverdraftProtection對象,它是能夠從其身份取決於支票帳戶如何 設置另一個帳戶轉移需要 資金彌補虧空。

如果我在網上查詢我找到自定義異常代碼是這樣的

public class DivisorCannotbeZeroException extends RuntimeException { 


    private static final long serialVersionUID = 1L; 

    public DivisorCannotbeZeroException(){ 
     super(); 
     System.out.println("I am doing something more"); 
    } 

    public DivisorCannotbeZeroException(String message){ 
     super(message); 
    } 

} 

即使打印語句是不是在該代碼工作。您能否請我解釋一下,關於如何爲自定義異常類添加更多功能以滿足我們的要求?

+1

print語句不工作如何繼承屬性?它怎麼能不行?你期待它做什麼?注意引用中的建議對我來說似乎是非常糟糕的設計。 – EJP 2014-12-03 08:46:18

+0

「不起作用」是什麼意思? – Fildor 2014-12-03 08:46:20

+1

個人而言,我會忽略整篇文章:我避免將過多的邏輯放入異常處理程序中:異常應該儘快將程序恢復到穩定狀態,而不是執行可能是「轉移資金」 *最後*我會在異常處理程序中做的事情。真的很瘋狂。 – Bathsheba 2014-12-03 08:49:27

回答

3

你必須構建Exception的一個實例(使用默認構造函數)爲構造函數(由throw通常是荷蘭國際集團吧)被調用,就像

public static void main(String[] args) { 
    throw new DivisorCannotbeZeroException(); 
} 

輸出是

I am doing something more 
Exception in thread "main" com.stackoverflow.Example 
    at com.stackoverflow.Example.main(Example.java:18) 
+0

確實糟糕的編碼。謝謝! – mvg 2014-12-03 08:54:50

0

MyException.java(用戶自定義)

package com.journaldev.exceptions; 

    public class MyException extends Exception { 

     private static final long serialVersionUID = 4664456874499611218L; 

     private String errorCode="Unknown_Exception"; 

     public MyException(String message, String errorCode){ 
      super(message); 
      this.errorCode=errorCode; 
     } 

     public String getErrorCode(){ 
      return this.errorCode; 
     } 
    } 

CustomException例

package com.journaldev.exceptions; 

    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.io.InputStream; 

    public class CustomExceptionExample { 

     public static void main(String[] args) throws MyException { 
      try { 
       processFile("file.txt"); 
      } catch (MyException e) { 
       processErrorCodes(e); 
      }  
     } 

     private static void processErrorCodes(MyException e) throws MyException { 
      switch(e.getErrorCode()){ 
      case "BAD_FILE_TYPE": 
       System.out.println("Bad File Type, notify user"); 
       throw e; 
      case "FILE_NOT_FOUND_EXCEPTION": 
       System.out.println("File Not Found, notify user"); 
       throw e; 
      case "FILE_CLOSE_EXCEPTION": 
       System.out.println("File Close failed, just log it."); 
       break; 
      default: 
       System.out.println("Unknown exception occured, lets log it for further debugging."+e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 

     private static void processFile(String file) throws MyException {  
      InputStream fis = null; 
      try { 
       fis = new FileInputStream(file); 
      } catch (FileNotFoundException e) { 
       throw new MyException(e.getMessage(),"FILE_NOT_FOUND_EXCEPTION"); 
      }finally{ 
       try { 
        if(fis !=null)fis.close(); 
       } catch (IOException e) { 
        throw new MyException(e.getMessage(),"FILE_CLOSE_EXCEPTION"); 
       } 
      } 
     } 

    } 

異常也可對其他類完成可以爲特定的過程