OlionkingO 2009-12-07 17:19
浏览 304
已采纳

java异常Throwable的问题,估计会的没几个。实话!!

有两个异常A和B,都是继承自Exception,在代码中捕获到A,底层抛的时候是封装成B往外抛的,当然还有一部分是B自己的异常,所以catch的时候只能捕获到B,但是我想判断到底是A还是B抛的,我怎么判断?那个Throwable里的cause怎么获取到,也就是说错误原因是在哪定义的,我如何获取到?真正的高手来。
[b]问题补充:[/b]
行不行呀?什么标题党呀?不会别瞎搀和。
[b]问题补充:[/b]
是我的描述有问题吧,你们的答案不是一个意境下的。
我给你们段代码,请高手帮我解决下。[code="java"]package temp;

public class BizException extends RuntimeException {

public BizException() {
    super();
    // TODO Auto-generated constructor stub
}

public BizException(String arg0, Throwable arg1) {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub
}

public BizException(String arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

public BizException(Throwable arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

}
[/code][code="java"]package temp;

public class ExceptionA extends BizException {

public ExceptionA() {
    super();
    // TODO Auto-generated constructor stub
}

public ExceptionA(String arg0, Throwable arg1) {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub
}

public ExceptionA(String arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

public ExceptionA(Throwable arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

}
[code="java"][/code]package temp;

public class ExceptionB extends BizException {

public ExceptionB() {
    super();
    // TODO Auto-generated constructor stub
}

public ExceptionB(String arg0, Throwable arg1) {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub
}

public ExceptionB(String arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

public ExceptionB(Throwable arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

}
[code="java"][/code]package temp;

public class Test {

private void a() {
    System.out.println("mmmmmm");
    throw new RuntimeException("..................");
}

private void test1() throws ExceptionA, ExceptionB {
    int i = 1;
    if (i == 0) {
        throw new ExceptionA();
    } else {
        throw new ExceptionB();
    }
}

public static void main(String[] args) {
    new Test().test2();
}

private void test2() throws BizException {
    try {
        new Test().test1();
    } catch (BizException e) {
        throw new BizException(e);
    }
}

}
[/code]
我要在main()里捕获,但是已经被封装成BizException了。
[b]问题补充:[/b]
看来你是个高手,真是环境是这样的。
高手再看看。
[code="java"]package temp;

public class A {

public A() throws ExceptionA,ExceptionC {
    super();
    // TODO Auto-generated constructor stub
}

}
[/code]
[code="java"]package temp;

public class ExceptionA extends Exception {
private Throwable cause;

/**
 * Constructs a JSONException with an explanatory message.
 * 
 * @param message
 *            Detail about the reason for the exception.
 */
public ExceptionA(String message) {
    super(message);
}

public ExceptionA(Throwable t) {
    super(t.getMessage());
    this.cause = t;
}

public Throwable getCause() {
    return this.cause;
}

}
[/code]
[code="java"]package temp;

public class ExceptionB extends Exception {
public ExceptionB() {
}

/**
 * @param message
 */
public ExceptionB(String message) {
    super(message);
}

/**
 * @param cause
 */
public ExceptionB(Throwable cause) {
    super(cause);
}

/**
 * @param message
 * @param cause
 */
public ExceptionB(String message, Throwable cause) {
    super(message, cause);
}

}
[/code][code="java"]package temp;

public class ExceptionB extends Exception {
public ExceptionB() {
}

/**
 * @param message
 */
public ExceptionB(String message) {
    super(message);
}

/**
 * @param cause
 */
public ExceptionB(Throwable cause) {
    super(cause);
}

/**
 * @param message
 * @param cause
 */
public ExceptionB(String message, Throwable cause) {
    super(message, cause);
}

}
[/code][code="java"]package temp;

public class ExceptionC extends Exception {
private Throwable cause;

/**
 * Constructs a JSONException with an explanatory message.
 * 
 * @param message
 *            Detail about the reason for the exception.
 */
public ExceptionC(String message) {
    super(message);
}

public ExceptionC(Throwable t) {
    super(t.getMessage());
    this.cause = t;
}

public Throwable getCause() {
    return this.cause;
}

}[/code][code="java"]package temp;

public class Test {

public static void main(String[] args) {

    try {
        new Test().nevv();
    } catch (ExceptionB e) {
        System.out.println(e.getCause());
    }
}

public void nevv() throws ExceptionB {
    try {
        new A();
    } catch (ExceptionA e) {
        throw new ExceptionB(e);
    }catch (ExceptionC e) {
        throw new ExceptionB(e);
    }
}

}
[/code]
这个怎么辨别?上面三个异常都不能改,我就是在捕获的时候要分辨出来。

  • 写回答

9条回答 默认 最新

  • wocsok 2009-12-08 13:08
    关注

    public class Test{

    public static void main(String[] args) {   
    
        try {   
            new Test().nevv();   
        } catch (ExceptionB e) {   
            if(e.getCause()==null){
                 System.out.println("异常B");  
            }else if(e.getCause() instanceof ExceptionA){
                 System.out.println("异常A");  
            }else if(e.getCause() instanceof ExceptionC){
                 System.out.println("异常C");  
            }
    
        }   
    }   
    
    public void nevv() throws ExceptionB {   
        try {   
            new A();   
        } catch (ExceptionA e) {   
            throw new ExceptionB(e);   
        }catch (ExceptionC e) {   
            throw new ExceptionB(e);   
        }   
    }   
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型