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]
这个怎么辨别?上面三个异常都不能改,我就是在捕获的时候要分辨出来。
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) 8)
哈哈哈 纯标题党 :x :x
我不是什么高手 这是JAVA基础啊 还有你内个标题太邪乎了吧 给你段代码吧
public class TESTTEST {
public static void main(String[] args) {
try {
try {
throw new SQLException("sql异常");
} catch (SQLException se) {
IOException er = new IOException("IO异常");
er.initCause(se);
throw er;
}
// throw new IOException("sql异常");
[align=center][/align]
} catch (IOException io) {
if (io.getCause() != null) {
System.out.println("###嵌套SQL异常的IO异常####");
System.out.println(io.getCause());
System.out.println("###嵌套SQL异常的IO异常####");
}else{
System.out.println("###原始IO异常####");
System.out.println(io.getCause());
System.out.println("###原始IO异常####");
}
}
}
}先运行程序 然后再把注释掉的恢复 把上面的try catch注视掉
你从结果就能看出如何区分 一般异常都是这么嵌套处理的啊 你这个就是判断下getCause()是否为空 如果为空证明它里面没嵌套别的异常[align=center][/align]
import java.io.IOException;
import java.sql.SQLException;
public class TESTTEST {
public static void main(String[] args) {
try {
try {
throw new SQLException("sql异常");
} catch (SQLException se) {
IOException er = new IOException("IO异常");
er.initCause(se);
throw er;
}
//throw new IOException("sql异常");
} catch (IOException io) {
if (io.getCause() != null) {
System.out.println("###嵌套SQL异常的IO异常####");
System.out.println(io.getCause());
System.out.println("###嵌套SQL异常的IO异常####");
}else{
System.out.println("###原始IO异常####");
System.out.println(io.getCause());
System.out.println("###原始IO异常####");
}
}
}
}
Exception也是一个类,可以使用instance of 来做判定
这不还是我给你的答案那意思么
你现在想要的是不是在main方法里判断
new Test().test2(); 抛出的异常到底是exceptionA 还是exceptionB 或者是bizexception么?
private void test1() throws ExceptionA, ExceptionB {
int i = 2;
if (i == 0) {
throw new ExceptionA();
} else if(i== 1){
throw new ExceptionB();
} else{
throw new BizException();
}
}
public static void main(String[] args) {
try{
new Test1().test2();
}catch(BizException be){
System.out.println(be.getCause());
}
}
你把这个两个类替换掉你的那两个类,把i分别输入0,1,2不就行了么 你运行下就
行 只要异常里面包含别的异常 它的getCause() 就会返回它包含的 后则就会返回为空
instance of 可以判断两个类之间是否有继承关系
[table]
|[table]|
||[table]||
|||[table]|||
||||[table]||||
|||||[table]|||||
||||||[table]||||||
|||||||[table]|||||||
||||||||[table]||||||||
|||||||||[flash=200,200][url][img][list]|||||||||
|||||||||[*][code="java"][quote][u][i][b][/b][/i][/u][/quote][/code]|||||||||
|||||||||[/list][/img][/url][/flash]|||||||||
||||||||[/table]||||||||
|||||||[/table]|||||||
||||||[/table]||||||
|||||[/table]|||||
||||[/table]||||
|||[/table]|||
||[/table]||
|[/table]|
[/table]