为什么从 finally 块抛出的异常会忽略从 catch 块抛出的异常?
Why an exception thrown from finally block ignores an exception thrown from catch block?
谁能告诉我为什么 new Exception()
在这里被忽略的原因?
void bar() throws IOException { //no Exception declared, compilator is ok about that. Why?
try{
throw new EOFException();
}
catch(EOFException eofe){
throw new Exception();
}
finally{
throw new IOException();
}
}
finally块总是被执行,不管try块是否抛出异常(也不管它是否有return语句)。
因此,finally 块抛出的异常 - IOException
- 是你的方法抛出的唯一异常,并且它总是抛出,无论 try 块的内容如何。因此,您的方法只需声明它 throws IOException
.
谁能告诉我为什么 new Exception()
在这里被忽略的原因?
void bar() throws IOException { //no Exception declared, compilator is ok about that. Why?
try{
throw new EOFException();
}
catch(EOFException eofe){
throw new Exception();
}
finally{
throw new IOException();
}
}
finally块总是被执行,不管try块是否抛出异常(也不管它是否有return语句)。
因此,finally 块抛出的异常 - IOException
- 是你的方法抛出的唯一异常,并且它总是抛出,无论 try 块的内容如何。因此,您的方法只需声明它 throws IOException
.