fork(1) download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static List<Integer> wtf() {
  5. List<Integer> forty_two = new ArrayList<Integer>();
  6. forty_two.add(42);
  7.  
  8. try {
  9. System.out.println("trying to return: " + forty_two);
  10. return forty_two;
  11. }
  12. catch(Exception e) {
  13. System.out.println(e.toString());
  14. }
  15. finally {
  16. forty_two.clear();
  17.  
  18. System.out.println("actually returning: " + forty_two);
  19. return forty_two;
  20. }
  21. }
  22.  
  23. public static List<Integer> wtfff() {
  24. List<Integer> forty_two = new ArrayList<Integer>();
  25. forty_two.add(42);
  26.  
  27. List<Integer> twenty_three = new ArrayList<Integer>();
  28. twenty_three.add(23);
  29.  
  30. try {
  31. System.out.println("trying to return: " + forty_two);
  32. return forty_two;
  33. }
  34. catch(Exception e) {
  35. System.out.println(e.toString());
  36. }
  37. finally {
  38. System.out.println("actually returning: " + twenty_three);
  39. return twenty_three;
  40. }
  41. }
  42.  
  43. public static void main(String[] args) {
  44. System.out.println("'finally' can modify a variable being returned inside a 'try' block:");
  45. wtf();
  46.  
  47. System.out.println();
  48.  
  49. System.out.println("'finally' can also change which variable is actually returned:");
  50. wtfff();
  51. }
  52. }
Success #stdin #stdout 0.1s 35896KB
stdin
Standard input is empty
stdout
'finally' can modify a variable being returned inside a 'try' block:
trying to return: [42]
actually returning: []

'finally' can also change which variable is actually returned:
trying to return: [42]
actually returning: [23]