RuntimeException#
You never need to write an exception specification saying that a method might throw a RuntimeException,
because they are unchecked exceptions. it's dealt with automatically.Keep in mind that only exceptions of type RuntimeException can be ignored in your coding
finally return之前仍然會(huì)執(zhí)行
Pitfall#
- It's possible for an exception to simply be lost.
This happens with a particular configuration using a finally clause
public static void main(String[] args) {
try {
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose(); //throw another exception will be
replace the former exception
}
} catch(Exception e) {
System.out.println(e);
}
}
- An even simpler way to lose an exception is just to return from inside a finally clause:
public static void main(String[] args) {
try {
throw new RuntimeException();
} finally {
// Using ‘return’ inside the finally block
// will silence any thrown exception.
return;
}
}