fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class CustomException extends Exception {
  8. public CustomException(String message) {
  9. super(message);
  10. }
  11. }
  12. class Ideone {
  13.  
  14. public static void main(String[] args) {
  15. try {
  16. System.out.println("Result: " + testMethod());
  17. } catch (Exception e) {
  18. System.out.println("Caught Exception in main: " + e.getMessage());
  19. }
  20. }
  21.  
  22. public static int testMethod() throws CustomException {
  23. int result = 0;
  24. try {
  25. result = 10;
  26. if (result == 10) {
  27. throw new CustomException("Custom exception thrown.");
  28. }
  29. result = 20;
  30. return result;
  31. } catch (CustomException e) {
  32. System.out.println("Caught CustomException: " + e.getMessage());
  33. result = 30;
  34. return result;
  35. } catch (Exception e) {
  36. System.out.println("Caught Exception: " + e.getMessage());
  37. result = 40;
  38. return result;
  39. } finally {
  40. System.out.println("In finally block of testMethod.");
  41. result = 50;
  42. }
  43. }
  44. }
  45.  
  46. // Candidate's' response
  47. //Custom exception thrown.
  48. //"Caught CustomException: "
  49. //"In finally block of testMethod."
  50. //"Result: " 20
  51. //
Success #stdin #stdout 0.16s 57600KB
stdin
Standard input is empty
stdout
Caught CustomException: Custom exception thrown.
In finally block of testMethod.
Result: 30