有两个异常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]
这个怎么辨别?上面三个异常都不能改,我就是在捕获的时候要分辨出来。