我有一个声明,引发了很多检查异常。我可以添加所有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);
}
这将是确定的,但我希望所有运行时异常被丢弃而不被卡住。有没有解决这个问题的方法?我在想,要捕捉的异常类型的一些聪明的泛型声明可能会诀窍(或者可能不会)。
很好的答案... +1 – Juvanis
+1很酷。不是我见过的最干净的,但是有诀窍。 – drasto