finally 中抛出异常处理

在finally 不要return 

main:System.out.println(testExp());
    public static String  testExp() {
        try {
            int i = 0;
            int resutl = i / 0;
            return "suc";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("exp1");
            return "error1";
        } finally {
            try {
                int r = 0 / 0;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("exp2");
            }
        }
    }
java.lang.ArithmeticException: / by zero
	at StreamStrTest.testExp(StreamStrTest.java:73)
	at StreamStrTest.main(StreamStrTest.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
java.lang.ArithmeticException: / by zero
	at StreamStrTest.testExp(StreamStrTest.java:81)
	at StreamStrTest.main(StreamStrTest.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
exp1
exp2
error1

 public static String  testExp() {
        try {
            int i = 0;
            int resutl = i / 0;
            return "suc";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("exp1");
            return "error1";
        } finally {
            try {
                int r = 0 / 0;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("exp2");
              return "error2";
            }
        }
    }

exp1
java.lang.ArithmeticException: / by zero
exp2
	at StreamStrTest.testExp(StreamStrTest.java:73)
error2
	at StreamStrTest.main(StreamStrTest.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
java.lang.ArithmeticException: / by zero
	at StreamStrTest.testExp(StreamStrTest.java:81)
	at StreamStrTest.main(StreamStrTest.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值