Of course, Java is also flawed place. As a sign of abnormal program error, and it should never be ignored, but it is likely to be easily ignored.
Use the finally clause with some special way, it will happen:
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);
}
}
}
Output:
From the resulting output, we can see: VeryImportantException disappeared, it was replaced finally clause of HoHumException.
This is a very serious drawback, because exceptions may be lost in a more subtle than the previous example and shown in imperceptible ways.
An easier way to lose abnormal return from the finally clause:
After running this program, we can see that even if an exception is thrown, it will not produce any output.
public class ExceptionSilencer {
public static void main(String[] args) {
try {
throw new RuntimeException();
}finally {
return;
}
}
}
thank you all!