当前位置: 代码迷 >> Java相关 >> 小异常问题?
  详细解决方案

小异常问题?

热度:164   发布时间:2007-06-13 17:16:55.0
小异常问题?

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
谢了!!!!!!

搜索更多相关的解决方案: class  super  package  public  

----------------解决方案--------------------------------------------------------

你明白了类的初始化顺序就知道了

先是调用父类的构造函数的,这个时候 ,异常就已经抛出来了


----------------解决方案--------------------------------------------------------
明白,谢谢!呵呵~~~~~~~~~~
----------------解决方案--------------------------------------------------------
  相关解决方案