1 java exception-handling try-catch
我知道在这个网站上已经有很多关于try-catch-finally块的问题了.但我有一个不同的疑问.当下面的代码多次运行时,我得到不同的输出.
我有一个非常简单的类如下:
Practice.java
public class Practice {
public static void main(String []args) {
System.out.println(getInteger());
}
public static int getInteger() {
try {
System.out.println("Try");
throwException();
return 1;
} catch(Exception e) {
System.out.println("Catch Exception");
e.printStackTrace();
return 2;
} finally {
System.out.println("Finally");
}
}
private static void throwException() throws Exception {
throw new Exception("my exception");
}
}
Run Code Online (Sandbox Code Playgroud)
我第一次运行上面代码时的输出如下:
Try
Catch Exception
Finally
2
java.lang.Exception: my exception
at exceptionHandling.Practice.throwException(Practice.java:22)
at exceptionHandling.Practice.getInteger(Practice.java:10)
at exceptionHandling.Practice.main(Practice.java:4)
Run Code Online (Sandbox Code Playgroud)
下面再次运行代码时输出不同的输出:
Try
Catch Exception
java.lang.Exception: my exception
at exceptionHandling.Practice.throwException(Practice.java:22)
at exceptionHandling.Practice.getInteger(Practice.java:10)
at exceptionHandling.Practice.main(Practice.java:4)
Finally
2
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这样的输出吗?