一、自定义异常类
public class CustomExceptions extends Exception {
//空构造函数
public CustomException(){
super();
}
//定义异常信息
public CustomException(String message) {
super(message);
}
//定义异常信息,并保留上一层异常信息
public CustomException(String message,Throwable cause) {
super(message,cause);
}
//保留上一层异常信息
public CustomException(Throwable cause) {
super(cause);
}
}
二、处理抛异常的函数
//底层测试函数
public void func2() throws Exception {
throw new Exception("func2 exception ....");
}
//上层测试函数
public void func1() throws Exception {
try {
func2();
} catch (Exception ex) {
throw new CustomeException("func1 exception",ex);
}
}
三、测试端
//客户端测试函数
public static void main(String[] args) {
ExceptionChainTest test = new ExceptionChainTest();
try {
test.func1();
} catch (Exception e) {
e.printStackTrace();
}
}