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. 例外の根本原因を特定するには、メッセージ文字列を常にチェックする必要があります。
  2. 例外の種類の分離なし

アドバンテージ:

  1. コードが少ない。(コードは最小化できます)

そのような仕組みを採用すべきかどうか教えてください。

4

1 に答える 1

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 オブジェクトをキャッチして "THROWING" することで、エラーであっても適切に処理されることが保証されます。【Throwableオブジェクトを抑圧しないことが重要】

于 2013-04-25T11:12:39.860 に答える