Muk*_*rov 0 java exception-handling
谁能给我一个理由,为什么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();
}
}
Run Code Online (Sandbox Code Playgroud)
始终执行finally块,无论try块是否抛出异常(并且无论它是否具有return语句).
因此,finally块抛出的异常IOException
- 是您的方法抛出的唯一异常,并且无论try块的内容如何,它总是被抛出.因此,您的方法只需要声明它throws IOException
.