【Java學習筆記】自訂Java異常

來源:互聯網
上載者:User

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

1.前言:

你的程式總有一天會崩潰掉,在崩潰掉的時候我們要知道它在哪,為了什麼而崩潰掉,資料的儲存或者丟失情況如何等問題。我們可以通過繼承類java.lang.Throwable的子類:Exception來設計我們自己的Java異常。Exception類用於描述程式能夠捕獲的異常,如ClassNotFoundException。要注意的是自訂異常類之間也可以有繼承關係,同時也需要為自訂異常類設計構造方法,以方便構造自訂異常對象。

2.設計執行個體分析:
這是個比較完整的自訂異常類的設計,其實是比較模板化的東西。

package playground;
import java.io.*;

public class MyException extends Exception {
  private int id; // a unique id
  private String classname; // the name of the class
  private String method; // the name of the method
  private String message; // a detailed message
  private MyException previous =
   null; // the exception which was caught
  private String separator = "/n"; // line separator
  public MyException(int id, String classname, String method,
    String message, MyException previous) {
    this.id        = id;
    this.classname = classname;
    this.method    = method;
    this.message   = message;
    this.previous  = previous;
  } 
  public String traceBack() {
    return traceBack("/n");
  }

  public String traceBack(String sep) {
    this.separator = sep;
    int level = 0;
    MyException e = this;
    String text = line("Calling sequence (top to bottom)");
    while (e != null) {
      level++;
      text += line("--level " + level + "--------------------------------------");
      text += line("Class/Method: " + e.classname + "/" + e.method);
      text += line("Id          : " + e.id);
      text += line("Message     : " + e.message);
      e = e.previous;
    } 
    return text;
  }

  private String line(String s) {
    return s + separator;
  }

}

我們來一個個看看這些東西:
在這個繼承了Exception類的自訂異常類中,我們定義了如下變數:
id:獨立標示符,這個是用來進行標示類中什麼地方出現了錯誤被捕捉到。
classname:捕捉到這個錯誤的類的名字。
method:捕捉到這個錯誤的方法的名字。
message:用來描述整個事件的情況。
previous:是MyException得一個執行個體,若在鏈表中它是第一個的話,那麼它就是null。

我們再看看都定義了什麼方法:
traceBack():產生一個包含在異常類中儲存的資料的一個回溯。使用newline標示作為分隔字元
traceBack(String sep):和上一個其實是相同的東西,使用的分隔字元可以自己定義。
line(String s):traceBack使用的一個方法。

步驟一:先扔出第一個異常:
這有兩種情況:
a.若程式判斷異常的某個地方主動拋出異常,那麼你就會主動拋出一個錯誤,例如:
throw new MyException(  1, "Person", "getSalary",
  "Trying to get a salary, but no person was specified",null);

b.在另一個異常出現時拋出該異常,例如:
catch (Exception e) {
  throw new MyException(4, "Person", "read", e.toString(), null);
}

要是某一個方法要拋出MyException異常,那麼在方法定義的時候形式如下:
public void read() throws MyException . . .

步驟二:接著我們扔出來的異常,例如:
Person p = new Person();
p.setPersonId(id);
try {
  p.read();
  s += p.getSalary();

catch (MyException e) {
  throw new MyException(1, "Stats", "getAllSalaries",
    "Could not get salary for " + id, e);
}

我們的策略是這樣的:將我們可能扔出異常的方法置於我們的監控之下(try{}),然後出了事情我們在處理區(catch{})把這個異常處理,至於處理的方式可以是當場處理,也可以處理不了交予其他地方處理(throw())。需要注意的是,我們看到最後一個參數e,我們要在每個新的異常處將這個異常加入到異常類的鏈表中。

步驟三:回溯異常
我們上邊設計的這個異常最後得出的一個異常鏈如下:
Calling sequence (top to bottom)
--level 1--------------------------------------
Class/Method: SalaryServlet/doGet
Id : 7
Message : Trying to get the total salary for employees
--level 2--------------------------------------
Class/Method: Stats/getAllSalaries
Id : 1
Message : Could not get salary for Lincoln
--level 3--------------------------------------
Class/Method: Person/read
Id : 3
Message : java.sql.SQLException: [HANSEN]Invalid object name 'xEMPLOYEE'.
--level 4--------------------------------------
Class/Method: Person/read
Id : 999
Message : SQLException. State/error code: S0002/208

我們可以分析得知在這個情境上,一個servlet被觸發了,它調用了getAllSalaries,然後又調用了read方法,在read方法中,出現了一個SQLException

3.總結:
自訂異常類給了我們自己喜歡的異常的出現,其他的也沒什麼。哈哈~

 

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.