class TestExceptions { public static void main(String[] args) throws Exception { try { System.out.println("try"); throw new Exception(); } catch(Exception e) { System.out.println("catch"); throw new RuntimeException(); } finally { System.out.println("finally"); } } }
Below are the results when I try to run the code in eclipse several times. I still thought that whenever the last line of code from the try / catch block (which can be returned or throws a new Exception () stmt type) is completed, the finally block will be executed, but is the output different every time? Can anyone clarify whether my assumption is right or wrong?
try catch Exception in thread "main" finally java.lang.RuntimeException at TestExceptions.main(TestExceptions.java:9) Exception in thread "main" try catch java.lang.RuntimeException at TestExceptions.main(TestExceptions.java:9) finally
java exception
Dish
source share