2013-04-25 94 views
0

我在下面的代碼中找到異常處理的代碼,該異常處理處理包括運行時在內的應用程序拋出的所有可執行文件。通用異常處理

 public static void handleException(String strMethodName, 
        Exception ex) throws CustomException{ 
      String strMessage = ""; 
      try { 
       throw ex; 
      } 
      catch (NullPointerException npe){ 
       logger.log(pr, strMethodName, npe.getMessage()); 
       strMessage=ERR_02; 
       throw new CustomException(strMessage); 
      } 
      catch(IndexOutOfBoundsException iobe) { 
       logger.logMethodException(strMethodName,iobe.getMessage()); 
       strMessage=ERR_03; 
       throw new CustomException(strMessage); 
      } 
      ... So On 
    } 

下面是一些我認爲不足之處:

  1. 要idenitify我們將需要經常檢查的消息字符串異常的根本原因。

    1. 更少的代碼:
    2. 型異常

    優勢的無sepearation。 (代碼可以最小化)

請您告訴我是否應該使用這種機制。

回答

1

不確定您使用該代碼的情況。

在你的方法,你是不是重新拋出它可用於調試

public static void handleException(String strMethodName, 
        Throwable th){ 
      String strMessage = ""; 
      try { 
       throw th; 
      } 
      catch (Throwable thr){ 
       logger.log(pr, strMethodName, npe.getMessage()); 
       //get the appropriate error code from a method 
       strMessage=getErrorCode(); 
       throw new CustomException(strMessage, th); 
       //CustomException is of type RuntimeException 
      } 
    } 

通過捕獲和「扔」的Throwable對象你確信即使錯誤得到妥善處理異常對象。 [重要的是不要壓制Throwable對象]