Jul*_*ien 6 java exception rethrow java-7
更精确的重新抛出允许编写抛出异常抛出的异常的代码:
public void foo(String bar) throws FirstException, SecondException {
try{
// Code that may throw both FirstException and SecondException
}
catch (Exception e){
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
在Java 7之前,你必须写:
public void foo(String bar) throws Exception {
try{
// Code that may throw both FirstException and SecondException
}
catch (Exception e){
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:是否有一个工具允许检测不精确的投掷,以便用" Exception" 替换" FirstException, SecondException"?
到目前为止,我已经检查过Eclipse中没有编译器警告.FindBugs或CodePro中没有规则.
Han*_* R. -2
尝试在抛出之前强制转换异常,也许可以解决问题?
public void foo(String bar) throws FirstException, SecondException {
try{
// Code that may throw both FirstException and SecondException
}
catch (Exception e){
throw ((e instanceof FirstException) ? (FirstException)e : (SecondException)e);
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,重新抛出相同的异常对我来说似乎相当笨拙......