View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package integration.tests.other.control.structures;
7   
8   public final class TryCatchFinallyStatements {
9       void tryCatch() {
10          try {
11              System.gc();
12          } catch (Exception e) {
13              e.printStackTrace();
14          }
15      }
16  
17      boolean tryCatchWhichThrowsAndCatchesException() {
18          try {
19              throw new RuntimeException("testing");
20          } catch (RuntimeException e) {
21              return true;
22          }
23      }
24  
25      int regularTryFinally(boolean b) {
26          try {
27              if (b) {
28                  return 1;
29              }
30              return 0;
31          } finally {
32              System.gc();
33          }
34      }
35  
36      // very different from javac with Eclipse compiler
37      boolean finallyBlockWhichCannotCompleteNormally(boolean b) {
38          while (b) {
39              try {
40                  return true;
41              } finally {
42                  break;
43              }
44          }
45          return false;
46      }
47  
48      int whileTrueWithTryFinallyInsideAnother() {
49          int i = 0;
50          while (true) {
51              try {
52                  try {
53                      i = 1;
54                      // the first finally clause
55                  } finally {
56                      i = 2;
57                  }
58                  i = 3;
59                  // this never completes, because of the continue
60                  return i;
61                  // the second finally clause
62              } finally {
63                  if (i == 3) {
64                      // this continue overrides the return statement
65                      continue;
66                  }
67              }
68          }
69      }
70  
71      void finallyBlockContainingIfWithBodyInSameLine() {
72          boolean b = false;
73  
74          try {
75              toString();
76          } finally {
77              if (b) {
78                  toString();
79              }
80          }
81      }
82  }