THINKING IN JAVA 上的一道关于异常处理的题目
class AbortedConstruction extends Exception {
public AbortedConstruction() {
super("Construction aborted");
}
}
class WithCleanup {
private boolean constructed = false;
public WithCleanup(boolean abort)
throws AbortedConstruction {
// Perform construction that might be
// unsucessful (and throw an exception) here.
if(abort) throw new AbortedConstruction();
System.out.println("After exception");
constructed = true;
}
public void cleanup() {
System.out.println(
"constructed = " + constructed);
if(constructed == true)
System.out.println("Cleaning up");
else
System.out.println(
"Constructor didn't finish,"+
"not cleaning up");
}
}
public class E14_CleanupFlag {
public static void main(String args[]) {
WithCleanup wc = null;
try {
wc = new WithCleanup(false);
} catch(AbortedConstruction e) {
System.out.println("Caught " + e);
} finally {
System.out.println(
"In finally 1, preparing to clean up");
wc.cleanup();
}
wc = null; // Very important!
try {
try {
wc = new WithCleanup(true);
} catch(AbortedConstruction e) {
System.out.println("Caught " + e);
} finally {
System.out.println(
"In finally 2, preparing to clean up");
wc.cleanup();
}
} catch(Exception e) {
System.out.println("Caught exception "+ e);
}
}
} ///:~
结果:
After exception
In finally 1, preparing to clean up
constructed = true
Cleaning up
Caught AbortedConstruction: Construction aborted
In finally 2, preparing to clean up
Caught exception java.lang.NullPointerException
我想知道Caught exception java.lang.NullPointerException怎么产生的,大家帮帮忙