fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main {
  5.  
  6. public static void throwException(boolean runtime) throws Exception {
  7. if (runtime == true) {
  8. throw new RuntimeException();
  9. }
  10. else {
  11. throw new Exception();
  12. }
  13. }
  14.  
  15. public static String method(boolean doThrowFromTry, boolean doThrowFromCatch, boolean runtime, boolean retFromFinally) throws Exception {
  16. try {
  17. if (doThrowFromTry == true) {
  18. throwException(runtime);
  19. }
  20.  
  21. } catch (Exception e) {
  22. System.out.println("------------------Exception caught in catch block of callee method");
  23. if (doThrowFromCatch) {
  24. throwException(runtime);
  25. }
  26.  
  27. } finally {
  28. if (retFromFinally) {
  29. return "returnFromFinally";
  30. }
  31. }
  32. if (!retFromFinally) {
  33. return "returnAfterFinally";
  34. }
  35. return "dummyReturnForCompiler";
  36. }
  37.  
  38.  
  39. public static String executeMethod(boolean doThrowFromTry, boolean doThrowFromCatch, boolean runtime, boolean retFromFinally) {
  40. System.out.println("doThrowFromTry " + doThrowFromTry);
  41. System.out.println("doThrowFromCatch " + doThrowFromCatch);
  42. System.out.println("runtime " + runtime);
  43. String retVal = "retVal not initialized";
  44. try {
  45. retVal = method(doThrowFromTry, doThrowFromCatch, runtime, retFromFinally);
  46. } catch (Exception e) {
  47. System.out.println("------------------Exception " + e.getClass().getSimpleName() + "caught in caller method");
  48. }
  49. return retVal;
  50.  
  51. }
  52.  
  53. public static void main(String[] args) {
  54. boolean[] val = new boolean[]{true, false};
  55.  
  56. for (int i = 0; i < val.length; i++) {
  57. for (int j = 0; j < val.length; j++) {
  58. for (int k = 0; k < val.length; k++) {
  59. System.out.println("method which returns from finally");
  60. System.out.println(executeMethod(val[i], val[j], val[k],true));
  61. System.out.println("\nmethod which does not return from finally");
  62. System.out.println(executeMethod(val[i], val[j], val[k],false));
  63. System.out.println("\n\n");
  64. }
  65. }
  66. }
  67.  
  68. }
  69. }
  70.  
Success #stdin #stdout 0.07s 380544KB
stdin
Standard input is empty
stdout
method which returns from finally
doThrowFromTry   true
doThrowFromCatch true
runtime          true
------------------Exception caught in catch block of callee method
returnFromFinally

method which does not return from finally
doThrowFromTry   true
doThrowFromCatch true
runtime          true
------------------Exception caught in catch block of callee method
------------------Exception RuntimeExceptioncaught in caller method
retVal not initialized



method which returns from finally
doThrowFromTry   true
doThrowFromCatch true
runtime          false
------------------Exception caught in catch block of callee method
returnFromFinally

method which does not return from finally
doThrowFromTry   true
doThrowFromCatch true
runtime          false
------------------Exception caught in catch block of callee method
------------------Exception Exceptioncaught in caller method
retVal not initialized



method which returns from finally
doThrowFromTry   true
doThrowFromCatch false
runtime          true
------------------Exception caught in catch block of callee method
returnFromFinally

method which does not return from finally
doThrowFromTry   true
doThrowFromCatch false
runtime          true
------------------Exception caught in catch block of callee method
returnAfterFinally



method which returns from finally
doThrowFromTry   true
doThrowFromCatch false
runtime          false
------------------Exception caught in catch block of callee method
returnFromFinally

method which does not return from finally
doThrowFromTry   true
doThrowFromCatch false
runtime          false
------------------Exception caught in catch block of callee method
returnAfterFinally



method which returns from finally
doThrowFromTry   false
doThrowFromCatch true
runtime          true
returnFromFinally

method which does not return from finally
doThrowFromTry   false
doThrowFromCatch true
runtime          true
returnAfterFinally



method which returns from finally
doThrowFromTry   false
doThrowFromCatch true
runtime          false
returnFromFinally

method which does not return from finally
doThrowFromTry   false
doThrowFromCatch true
runtime          false
returnAfterFinally



method which returns from finally
doThrowFromTry   false
doThrowFromCatch false
runtime          true
returnFromFinally

method which does not return from finally
doThrowFromTry   false
doThrowFromCatch false
runtime          true
returnAfterFinally



method which returns from finally
doThrowFromTry   false
doThrowFromCatch false
runtime          false
returnFromFinally

method which does not return from finally
doThrowFromTry   false
doThrowFromCatch false
runtime          false
returnAfterFinally