国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

EJB最佳實踐:構建更好的異常處理框架

2019-11-18 12:33:07
字體:
來源:轉載
供稿:網友

  嵌套的異常
  
  在設計可靠的異常處理方案時,要考慮的第一件事情就是對所謂的 低級或 系統級異常進行抽象化。這些核心 java 異常通常會報告網絡流量中的錯誤、JNDI 或 RMI 問題,或者是應用程序中的其它技術問題。 RemoteException 、 EJBException 和 NamingException 是企業 Java 編程中低級異常的常見例子。
  
  這些異常完全沒有任何意義,由 Web 層的客戶機接收時尤其輕易混淆。假如客戶機調用 purchase() 并接收到 NamingException ,那么它在解決這個異常時會一籌莫展。同時,應用程序代碼可能需要訪問這些異常中的信息,因此不能輕易地拋棄或忽略它們。
  
  答案是提供一類更有用的異常,它還包含低級異常。清單 1 演示了一個專為這一點設計的簡單 applicationException :
  
  清單 1. 嵌套的異常
  
  package com.ibm;
  import java.io.PRintStream;
  import java.io.PrintWriter;
  public class ApplicationException
  extends Exception
  {
  /** A wrapped Throwable */
  protected Throwable cause;
  public ApplicationException()
  {
  super("Error occurred in application.");
  }
  public ApplicationException
  (String message)
  {
  super(message);
  }
  public ApplicationException
  (String message, Throwable cause)
  {
  super(message);
  this.cause = cause;
  }
  // Created to match the
  JDK 1.4 Throwable method.
  public Throwable
  initCause(Throwable cause)
  {
  this.cause = cause;
  return cause;
  }
  public String getMessage()
  {
  // Get this exception's message.
  String msg = super.getMessage();
  Throwable parent = this;
  Throwable child;
  // Look for nested exceptions.
  while((child =
  getNestedException(parent)) != null)
  {
  // Get the child's message.
  String msg2 = child.getMessage();
  // If we found a message
  for the child exception,
  // we append it.
  if (msg2 != null)
  {
  if (msg != null)
  {
  msg += ": " + msg2;
  }
  else
  {
  msg = msg2;
  }
  }
  // Any nested ApplicationException
  will append its own
  // children, so we need to
  break out of here.
  if (child instanceof
  ApplicationException)
  {
  break;
  }
  parent = child;
  }
  // Return the completed message.
  return msg;
  }
  public void printStackTrace()
  {
  // Print the stack trace
  for this exception.
  
  super.printStackTrace();
  Throwable parent = this;
  Throwable child;
  // Print the stack trace for
  each nested exception.
  while((child = getNestedException
  (parent)) != null)
  {
  
  if (child != null)
  {
  System.err.print("Caused by: ");
  child.printStackTrace();
  
  if (child instanceof ApplicationException)
  {
  
  break;
  
  }
  parent = child;
  }
  }
  }
  
  public void printStackTrace(PrintStream s)
  {
  
  // Print the stack trace for this exception.
  super.printStackTrace(s);
  Throwable parent = this;
  Throwable child;
  // Print the stack trace
  for each nested exception.
  while((child = getNestedException
  (parent)) != null)
  {
  if (child != null)
  {
  s.print("Caused by: ");
  child.printStackTrace(s);
  if (child instanceof
  ApplicationException)
  {
  break;
  }
  parent = child;
  }
  }
  }
  public void printStackTrace(PrintWriter w)
  {
  // Print the stack trace
  for this exception.
  super.printStackTrace(w);
  Throwable parent = this;
  Throwable child;
  // Print the stack trace for
  each nested exception.
  while((child =
  getNestedException(parent)) != null)
  {
  if (child != null)
  {
  w.print("Caused by: ");
  child.printStackTrace(w);
  if (child instanceof
  ApplicationException)
  {
  break;
  }
  parent = child;
  }
  }
  }
  public Throwable getCause()
  {
  return cause;
  }
  }
  
  清單1中的代碼很簡單;我們已經簡單地將多個異常“串”在一起,以創建單個、嵌套的異常。但是,真正的好處在于將這種技術作為出發點,以創建特定于應用程序的異常層次結構。異常層次結構將答應 EJB 客戶機既接收特定于業務的異常也接收特定于系統的信息,而不需要編寫大量額外代碼。
  
  異常層次結構
  
  異常層次結構應該從一些十分健壯而又通用的異常入手,如 ApplicationException 。假如您將頂級異常搞得太具體,那么其結果是您今后將不得不重新構造層次結構,以適應某些較通用的情況。
  
  因此,讓我們假定您的應用程序要求 NoSUChBookException 、 InsufficientFundsException 和 SystemUnavailableException 。您不必創建這三個異常,讓它們繼續 ApplicationException ,然后只需提供極少幾個必須的構造器來創建格式化的消息。清單 2 是此類異常層次結構的示例:
  
  清單 2. 異常層次結構
  
  package com.ibm.library;
  import com.ibm.ApplicationException;
  public class NoSuchBookException
  extends ApplicationException
  {
  public NoSuchBookException
  (String bookName, String libraryName)
  {
  super("The book '" + bookName
  + "' was not found in the '" +
  libraryName + "' library.");
  }
  }
  
  當需要編寫大量專用異常時,異常層次結構極大地簡化了工作。對于一個異常,為每個異常類添加一個或兩個構造器,所花費時間很少不超過幾分鐘。您還經常需要給這些更具體的異常(這些異常也是主應用程序異常的子類)提供子類,以提供更具體的異常。例如,您可能需要 InvalidTitleException 和 BackorderedException 來繼續 NoSuchBookException 。
  
  企業應用程序在構建時通常都不會注重異常處理。盡管依靠低級異常(如 RemoteException 和 NamingException )很輕易(有時也很誘人),但假如一開始就建立一個可靠的、深思熟慮的異常模型,則您將在應用程序上少花很多精力。創建一個嵌套的、層次結構化的異常框架將改進代碼的可讀性及其可用性。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 胶州市| 唐山市| 苏州市| 封丘县| 若羌县| 台湾省| 岑巩县| 华蓥市| 田东县| 清河县| 佛坪县| 宁阳县| 边坝县| 巩留县| 讷河市| 弋阳县| 黄大仙区| 彰化市| 新巴尔虎左旗| 依兰县| 黎城县| 泊头市| 吴川市| 开鲁县| 广宗县| 舒兰市| 肇庆市| 清远市| 平顺县| 广平县| 镇赉县| 历史| 滦平县| 广昌县| 阳朔县| 石狮市| 大丰市| 城固县| 凤阳县| 班戈县| 临潭县|