2013-04-25 88 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对象]