【问题标题】:Java Try Catch ExceptionJava 尝试捕获异常
【发布时间】:2021-12-13 05:49:01
【问题描述】:

我当前的代码块返回如下异常:

Exception occurred in API invocation A1-123 Fatal error
Caused by: A9-001 ColName is not found in TableName

但我想...

  1. 摆脱A1 Exception
  2. 直接显示A9 Exception
  3. Caused by 中不显示A9 Exception

如何使异常看起来像这样?

Exception occurred in API invocation A9-001 ColName is not found in TableName
<no Caused by clause>

这是我的示例代码:

public Sample Method (Input input) throws AException
{
   con = getSQLConnection();

   try{
      //do something
      if(x==null){
         throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
      }
   }
   catch (Exception e){
      logger().error(e);
      throw new AException(e);
   }
   finally{
      if(con!=null){
         try{
            con.close();
         }
         catch(Exception e){
            logger().error(e);
            throw new AException(e);  
         }
      }
   }
}

如果是这样的话,它会起作用吗:

   try{
      //do something
   }
   catch (Exception e){
      logger().error(e);
      throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
   }
   catch (Exception e){
      logger().error(e);
      throw new AException(e);
   }

【问题讨论】:

    标签: java api exception error-handling try-catch


    【解决方案1】:

    第一个评论者是正确的。我能够通过创建一个新的 first catch 来做到这一点:

    public Sample Method (Input input) throws AException
    {
       con = getSQLConnection();
    
       try{
          //do something
          if(x==null){
             AException ae = new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
             logger().error(ae);
             throw ae;
          }
       }
       catch (AException ae){
          logger().error(ae);
          throw ae;
       }
       catch (Exception e){
          logger().error(e);
          throw new AException(e);
       }
       finally{
          if(con!=null){
             try{
                con.close();
             }
             catch(Exception e){
                logger().error(e);
                throw new AException(e);  
             }
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      我假设您要捕获并传递的错误是AException - 否则您所要求的将违反该方法的合同。您可以通过像这样的额外catch 子句来实现您想要的。

      try {
          // whatever
      }
      catch (AException ae) {
          throw ae;
      }
      catch (Exception e){
        logger().error(e);
        throw new AException(e);
      }
      finally {
           // whatever
      }
      

      这样,只有不属于 AException 类型的异常才会被包装在新的 AException 对象中。

      【讨论】:

      • 我是 try-catch 的新手。你的意思是说我当前的第一个捕获应该被视为第二个捕获然后我创建一个新的第一个捕获。但是我如何在第一次捕获中准确地实现throw new exception AException(A9Messages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"})? (我不明白这个表格:catch (AException ae) { throw ae; }
      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 2014-05-11
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多