在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)