package Java_Think;
class Except1 extends Exception {
public Except1(String s) {
super(s);
}
}
class BaseWithException {
public BaseWithException() throws Except1 {
throw new Except1(
"thrown by BaseWithException");
}
}
class DerivedWE extends BaseWithException {
// Gives compile error:
// unreported exception Except1
// ! public DerivedWE() {}
// Gives compile error: call to super must be
// first statement in constructor:
//! public DerivedWE() {
//! try {
//! super();
//! } catch(Except1 ex1) {
//! }
//! }
public DerivedWE() throws Except1 {
throw new Except1("feifei");
}
}
public class E10_ConstructorExceptions {
public static void main(String args[]) {
try {
new DerivedWE();
} catch(Except1 ex1) {
System.out.println("Caught " + ex1.getMessage());
}
}
}
为什么输出的结果是:Caught thrown by BaseWithException
而不是:Caught feifei
谢了!!!!!!
----------------解决方案--------------------------------------------------------
你明白了类的初始化顺序就知道了
先是调用父类的构造函数的,这个时候 ,异常就已经抛出来了
----------------解决方案--------------------------------------------------------
明白,谢谢!呵呵~~~~~~~~~~
----------------解决方案--------------------------------------------------------