Dar*_*ght 5 java exception-handling
在java中是否有任何替代方法可以在jdk1.6中使代码成为可能.我知道在jdk 1.7中也是可能的,但我坚持使用jdk1.6.
下面的代码可以捕获多个异常,我想处理这些异常并将其添加到数据库表.因为对于所有3个异常,我的异常处理逻辑将保持相同.我不想为多个catch块重复相同的代码.
try{
//do somthing here
}catch(CustomException1 ex1 | CustomException2 ex2 | CustomException3 ex3){
// Here goes common Exception handing logic.
}
Run Code Online (Sandbox Code Playgroud)
try{
//do somthing here
}catch(Exception e){
if(e instanceof CustomException1 || e instanceof CustomException2 || e instanceof CustomException3 ) {
// Here goes common Exception handing logic.
} else { throw e;}
Run Code Online (Sandbox Code Playgroud)
}
我认为没有其他选择.
这个语法是在 Java 1.7 中添加的,因为以前很难干净利落地做到这一点。
您可以做以下几件事: