【问题标题】:Is it possible to catch all exceptions except runtime exceptions?是否可以捕获除运行时异常之外的所有异常?
【发布时间】:2012-11-02 22:45:44
【问题描述】:

我有一个语句会引发大量检查异常。我可以像这样为它们添加所有的 catch 块:

try {
    methodThrowingALotOfDifferentExceptions();
} catch(IOException ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
} catch(ClassCastException ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
} catch...

我不喜欢这样,因为它们都以相同的方式处理,所以存在代码重复,而且还有很多代码要编写。反而可以赶上Exception:

try {
    methodThrowingALotOfDifferentExceptions();
} catch(Exception ex) {
    throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
}

没关系,但我希望所有运行时异常都被丢弃而不被捕获。有什么解决办法吗?我在想,一些聪明的关于要捕获的异常类型的通用声明可能会奏效(或者可能不会)。

【问题讨论】:

    标签: java exception generics try-catch runtimeexception


    【解决方案1】:

    您可以执行以下操作:

    try {
        methodThrowingALotOfDifferentExceptions();
    } catch(RuntimeException ex) {
        throw ex;
    } catch(Exception ex) {
        throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
    }
    

    【讨论】:

      【解决方案2】:

      如果您可以使用 Java 7,则可以使用 Multi-Catch

      try {
        methodThrowingALotOfDifferentExceptions();
      } catch(IOException|ClassCastException|... ex) {
        throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
      }
      

      【讨论】:

      • +1 提醒我注意 Java 7 中这个新的有价值的(并且早就应该使用的 IMO)功能
      • +1 出于这个原因,如果可以的话,我会使用 Java 7。但我不能,我很抱歉,因为这个 Multi-Catch 是完美的。我必须使用 Java 6 :(((
      【解决方案3】:

      您可以尝试这样的事情,基本上捕获所有内容,然后重新抛出 RuntimeException 如果它是该类的实例...

      try {
          methodThrowingALotOfDifferentExceptions();
      } catch(Exception ex) {
          if (ex instanceof RuntimeException){
              throw ex;
          }
          else {
              throw new MyCustomInitializationException("Class Resolver could not be initialized.", ex);
          }
      }
      

      看起来好像一遍又一遍地编写会很麻烦(并且不利于可维护性),我可能会将代码移到不同的类中,就像这样......

      public class CheckException {
          public static void check(Exception ex, String message) throws Exception{
              if (ex instanceof RuntimeException){
                  throw ex;
              }
              else {
                  throw new MyCustomInitializationException(message, ex);
              }
          }
      }
      

      并像这样在您的代码中使用它...

      try {
          methodThrowingALotOfDifferentExceptions();
      } catch(Exception ex) {
          CheckException.check(ex,"Class Resolver could not be initialized.");
      }
      

      注意我们传入了message,这样我们仍然可以自定义我们的MyCustomInitializationException

      【讨论】:

      • 如果我需要做几次,最后一个选项会很好,但那不是我的情况 - 我只需要使用一次。只有我会使用String 消息对静态方法进行参数化。
      • 您需要在代码中将ex 转换为RuntimeException
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2012-09-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多