工作中常常遇到方法的嵌套,方法中有各自的try-catch异常捕获,但是实际上并不能满足需要,如果遇到嵌套的结构,该如何设计
实例1:
public class testTryCatch {
public static void main(String[] args) {
try {
childMain();
System.out.println("这里是父类的代码块");
}catch (Exception e){
System.out.println("父try");
//这里是我线程要捕获的东西------------
e.printStackTrace();
}finally {
System.out.println("这里给我不管咋样执行");
}
}
public static void childMain() throws Exception{
try {
Map<String,String> map = new HashMap<String,String>();
map.put("1111","1111");
throw new RuntimeException();//这里模拟程序执行报错
}catch (Exception e){
System.out.println("子try");
e.printStackTrace();
}
}
}
执行结果:
子try
java.lang.RuntimeException
at com.bonc.properties.testTryCatch.childMain(testTryCatch.java:29)
at com.bonc.properties.testTryCatch.main(testTryCatch.java:14)
这里是父类的代码块
这里给我不管咋样执行
如上结果,如果主方法想在catch里执行一些操作,其实并不会执行,这个时候就需要修改子方法中的try-catch将异常向上抛出
代码如下:
public class testTryCatch {
public static void main(String[] args) {
try {
childMain();
System.out.println("这里是父类的代码块");
}catch (Exception e){
System.out.println("父try");
//这里是我线程要捕获的东西------------
e.printStackTrace();
}finally {
System.out.println("这里给我不管咋样执行");
}
}
public static void childMain() throws Exception{
try {
Map<String,String> map = new HashMap<String,String>();
map.put("1111","1111");
throw new RuntimeException();//这里模拟程序执行报错
}catch (Exception e){
System.out.println("子try");
e.printStackTrace();
throw new Exception();
}
}
}
执行结果如下:
子try
java.lang.RuntimeException
at com.bonc.properties.testTryCatch.childMain(testTryCatch.java:28)
at com.bonc.properties.testTryCatch.main(testTryCatch.java:14)
父try
java.lang.Exception
at com.bonc.properties.testTryCatch.childMain(testTryCatch.java:32)
at com.bonc.properties.testTryCatch.main(testTryCatch.java:14)
这里给我不管咋样执行
从打印的结果可以看出,父方法的catch虽然捕获到了异常,但是很粗略,并不能精确定位
因此再做一个修改:
新建异常类:
public class BoncExpection extends RuntimeException {
private static final long serialVersionUID = 0;
private String code;
private String msg;
public BoncExpection() {
super();
}
public BoncExpection(String code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
public class testTryCatch {
public static void main(String[] args) {
try {
childMain();
System.out.println("这里是父类的代码块");
}catch (BoncExpection e){
System.out.println("父try");
//这里是我线程要捕获的东西------------
System.out.println(e.getMsg());
e.printStackTrace();
}finally {
System.out.println("这里给我不管咋样执行");
}
}
public static void childMain() throws BoncExpection{
try {
Map<String,String> map = new HashMap<String,String>();
map.put("1111","1111");
throw new RuntimeException();//这里模拟程序执行报错
}catch (Exception e){
System.out.println("子try");
BoncExpection boncExpection = new BoncExpection();
boncExpection.setMsg(getExceptionMessage(e));
throw boncExpection;
}
}
public static String getExceptionMessage(Exception e){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String exception = baos.toString();
return exception;
}
}
执行结果:
子try
父try
java.lang.RuntimeException
at com.bonc.properties.testTryCatch.childMain(testTryCatch.java:33)
at com.bonc.properties.testTryCatch.main(testTryCatch.java:18)
com.bonc.common.utils.BoncExpection
at com.bonc.properties.testTryCatch.childMain(testTryCatch.java:37)
at com.bonc.properties.testTryCatch.main(testTryCatch.java:18)
这里给我不管咋样执行
如上结果,子方法的异常直接和父方法的异常一起打印,也方便查看,因此在实际开发中搭配灵活使用即可
版权声明:本文为博主原创文章,转载请注明本页地址。https://blog.csdn.net/l1994m/article/details/103407577