try-catch嵌套结构的异常捕获

工作中常常遇到方法的嵌套,方法中有各自的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员lm

感谢您的支持,后续分享更多知识

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值