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 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择