【发布时间】: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