Moo*_*ors 2 java exception-handling
这是代码:
public class Exc {
int x = 2;
public void throwE(int p) throws Excp, Excp2 {
if(x==p) {
throw new Excp();
}
else if(x==(p+2)) {
throw new Excp2();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是处理程序代码:
public class tdExc {
public static void main(String[] args) {
Exc testObj = new Exc();
try {
testObj.throwE(0);
System.out.println("This will never be printed, so sad...");
} catch(Exception Excp) {
System.out.println("Caught ya!");
} catch(Exception Excp2) {
System.out.println("Caught ya! Again!!!!");
} finally {
System.out.println("This will always be printed!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Excp并且Excp2都扩展Exception并具有类似的代码(没有).现在我得到错误Exception has already been caught错误Excp2,无论我是否向throwE方法提供2或0 .
您正在寻找:
try
{ }
catch(Excp excp)
{
log(excp);
}
catch(Excp2 excp2)
{
log(excp2);
}
finally
{ }
Run Code Online (Sandbox Code Playgroud)
捕获异常时,指定异常的类型及其引用的名称.
您的原始代码尝试捕获Exception,这是最不具体的异常,所以在此之后您无法捕获任何内容.
| 归档时间: |
|
| 查看次数: |
8620 次 |
| 最近记录: |