如果构造函数以异常结束,那么创建的对象是否与正常对象完全相同?
class A {
static A o;
A() throws Exception {
o=this;
throw new Exception();
}
void f() { System.out.println("f(): entry."); };
static public void main(String[]args ) {
try {
A o =new A();
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
A.o.f(); // Is it safe to use this object?
}
}
这编译并运行,这个程序的输出是:
Exception: java.lang.Exception
f(): entry.