您的位置:首页 > 其它

捕获异常然后抛出另一个异常发生了什么?

2009-11-09 18:54 483 查看
package com.lwf.thinking.eight;
import java.sql.SQLException;
public class UserDefineException {
public static void main(String[] args) {
try {
catchException();
} catch (EusException e) {
e.printStackTrace();
}
}

public static void catchException() throws EusException{
try {
throwException();
} catch (SQLException e) {
throw new EusException();
}

}
public static void throwException() throws SQLException{
System.out.println("throwException");
throw new SQLException();
}
}


对应的自定义异常类为:

package com.lwf.thinking.eight;
public class EusException extends Exception {
public EusException(){
super();
}

public EusException(String exception){
super(exception);
}

}


上述代码输出结果为:

throwException

com.lwf.thinking.eight.EusException

at com.lwf.thinking.eight.UserDefineException.catchException(UserDefineException.java:19)

at com.lwf.thinking.eight.UserDefineException.main(UserDefineException.java:9)

可以看到输出结果看不到最开始的throwException中抛出的SQLException,这是因为在catchException中抛出的是EusException而不是SQLException

那么当我抛出另一个异常的时候也想让这个异常包含引发它的异常怎么做呢?

修改异常类:增加方法

public EusException(Throwable cause){

super(cause);

}

对应异常类为:

package com.lwf.thinking.eight;
public class EusException extends Exception {
public EusException(){
super();
}

public EusException(String exception){
super(exception);
}

public EusException(Throwable cause){
super(cause);
}

}


代码改为:

throw new EusException(e);

如下:

package com.lwf.thinking.eight;
import java.sql.SQLException;
public class UserDefineException {
public static void main(String[] args) {
try {
catchException();
} catch (EusException e) {
e.printStackTrace();
}
}

public static void catchException() throws EusException{
try {
throwException();
} catch (SQLException e) {
throw new EusException(e);//修改处
}

}
public static void throwException() throws SQLException{
System.out.println("throwException");
throw new SQLException();
}
}


输出结果为:

throwException

com.lwf.thinking.eight.EusException: java.sql.SQLException

at com.lwf.thinking.eight.UserDefineException.catchException(UserDefineException.java:19)

at com.lwf.thinking.eight.UserDefineException.main(UserDefineException.java:9)

Caused by: java.sql.SQLException

at com.lwf.thinking.eight.UserDefineException.throwException(UserDefineException.java:25)

at com.lwf.thinking.eight.UserDefineException.catchException(UserDefineException.java:17)

... 1 more

可以看到结果列出了引发的原始异常SQLException。

注意,因为JAVA API中有很多类并没有参数为Throwable 类型的构造函数:

public EusException(Throwable cause){

super(cause);

}

但自Throwable类继承过来的类都有initCause(Throwable cause)方法,所以另一种实现的方式是:

异常类不变。

package com.lwf.thinking.eight;
import java.sql.SQLException;
public class UserDefineException {
public static void main(String[] args) {
try {
catchException();
} catch (EusException e) {
e.printStackTrace();
}
}

public static void catchException() throws EusException{
try {
throwException();
} catch (SQLException e) {
EusException eus = new EusException();
eus.initCause(e);
throw eus;
//另一种方法抛出包含原有异常信息
}

}
public static void throwException() throws SQLException{
System.out.println("throwException");
throw new SQLException();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: