もちろん、Javaはまた、欠陥のある場所です。異常なプログラムエラーの印として、それは無視すべきではありませんが、簡単に無視される可能性が高いです。
いくつかの特別な方法でfinally節を使用して、それが起こるのだろう。
class VeryImportantException extends Exception{
public String toString() {
return "A very important exception";
}
}
class HoHumException extends Exception{
public String toString() {
return "A trivial exception";
}
}
public class LostMessage {
void f() throws VeryImportantException{
throw new VeryImportantException();
}
void dispose() throws HoHumException{
throw new HoHumException();
}
public static void main(String[] args) {
try {
LostMessage lm = new LostMessage();
try {
lm.f();
}finally {
lm.dispose();
}
}catch(Exception e) {
System.out.println(e);
}
}
}
出力:
出力結果から、我々は見ることができます:VeryImportantExceptionはそれがHoHumExceptionのfinally節を交換し、姿を消しました。
例外は、前の例よりも、より微妙で失われたと感知できない方法で示すことができるので、これは、非常に重大な欠点です。
finally節から異常なリターンを失うする簡単な方法:
このプログラムを実行した後、私たちは、例外がスローされた場合でも、それは任意の出力を生成しないであろうことがわかります。
public class ExceptionSilencer {
public static void main(String[] args) {
try {
throw new RuntimeException();
}finally {
return;
}
}
}
ありがとうございます!