fork download
  1. class FinallyDemo {
  2. // Throw an exception out of the method.
  3. static void procA() throws IllegalAccessException{
  4. try {
  5. System.out.println("inside procA");
  6. throw new IllegalAccessException("demo");
  7. } finally {
  8. System.out.println("procA's finally");
  9. } }
  10. // Return from within a try block.
  11. static void procB() {
  12. try {
  13. System.out.println("inside procB");
  14. return;
  15. } finally {
  16. System.out.println("procB's finally");
  17. } }
  18. // Execute a try block normally.
  19. static void procC() {
  20. try {
  21. System.out.println("inside procC");
  22. } finally {
  23. System.out.println("procC's finally");
  24. } }
  25. public static void main(String args[]) {
  26. try {
  27. procA();
  28. } catch (IllegalAccessException e) {
  29. System.out.println("Exception caught");
  30. }
  31. procB();
  32. procC(); }
  33. }
  34.  
Success #stdin #stdout 0.09s 32532KB
stdin
Standard input is empty
stdout
inside procA
procA's finally
Exception caught
inside procB
procB's finally
inside procC
procC's finally