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)


发布了217 篇原创文章 · 获赞 127 · 访问量 89万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览