|
| 1 | +package testSuite.classes.try_catch_error; |
| 2 | + |
| 3 | +import liquidjava.specification.Refinement; |
| 4 | + |
| 5 | +public class Test { |
| 6 | + static void load() throws Exception { |
| 7 | + throw new Exception("x"); |
| 8 | + } |
| 9 | + |
| 10 | + // the try may complete normally, so t may be withThrowable |
| 11 | + static void stateOfTryPathKept() { |
| 12 | + Throwable t = new Throwable("start"); |
| 13 | + try { |
| 14 | + load(); |
| 15 | + t = new Throwable("try", new RuntimeException()); |
| 16 | + } catch (Exception e) { |
| 17 | + t = new Throwable("catch"); |
| 18 | + } |
| 19 | + t.initCause(new RuntimeException()); // Expect: State Refinement Error |
| 20 | + } |
| 21 | + |
| 22 | + // the catch may not run, so y may be negative |
| 23 | + static void valueOfTryPathKept() { |
| 24 | + int y = 0; |
| 25 | + try { |
| 26 | + load(); |
| 27 | + y = -5; |
| 28 | + } catch (Exception e) { |
| 29 | + y = 7; |
| 30 | + } |
| 31 | + @Refinement("_ > 0") |
| 32 | + int z = y; // Expect: Refinement Error |
| 33 | + } |
| 34 | + |
| 35 | + // the catch may run, so y may be negative |
| 36 | + static void valueOfCatchPathKept() { |
| 37 | + int y = 0; |
| 38 | + try { |
| 39 | + load(); |
| 40 | + y = 5; |
| 41 | + } catch (Exception e) { |
| 42 | + y = -5; |
| 43 | + } |
| 44 | + @Refinement("_ > 0") |
| 45 | + int z = y; // Expect: Refinement Error |
| 46 | + } |
| 47 | + |
| 48 | + // the exception may be thrown after t changed, so its state in the catch is unknown |
| 49 | + static void changedBeforeException() { |
| 50 | + Throwable t = new Throwable("start"); |
| 51 | + try { |
| 52 | + t.initCause(new RuntimeException()); |
| 53 | + load(); |
| 54 | + } catch (Exception e) { |
| 55 | + t.initCause(new RuntimeException()); // Expect: State Refinement Error |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // only the second of several catches assigns a negative value |
| 60 | + static void multipleCatches() { |
| 61 | + int y = 1; |
| 62 | + try { |
| 63 | + load(); |
| 64 | + } catch (RuntimeException e) { |
| 65 | + y = 3; |
| 66 | + } catch (Exception e) { |
| 67 | + y = -4; |
| 68 | + } |
| 69 | + @Refinement("_ > 0") |
| 70 | + int z = y; // Expect: Refinement Error |
| 71 | + } |
| 72 | + |
| 73 | + // a value assigned in a nested if of the try may reach the catch |
| 74 | + static void valueFromIfInTry(boolean b) { |
| 75 | + int y = 1; |
| 76 | + try { |
| 77 | + if (b) |
| 78 | + y = -1; |
| 79 | + load(); |
| 80 | + } catch (Exception e) { |
| 81 | + @Refinement("_ > 0") |
| 82 | + int z = y; // Expect: Refinement Error |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // the finally also runs after a catch that returns |
| 87 | + static void finallyAfterReturningCatch() { |
| 88 | + Throwable t = new Throwable("start"); |
| 89 | + try { |
| 90 | + load(); |
| 91 | + return; |
| 92 | + } catch (Exception e) { |
| 93 | + t.initCause(e); |
| 94 | + return; |
| 95 | + } finally { |
| 96 | + t.initCause(new RuntimeException()); // Expect: State Refinement Error |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // the finally also runs when an exception leaves the try uncaught |
| 101 | + static void finallyAfterUncaughtException() throws Exception { |
| 102 | + Throwable t = new Throwable("start"); |
| 103 | + try { |
| 104 | + t.initCause(new RuntimeException()); |
| 105 | + load(); |
| 106 | + t = new Throwable("again"); |
| 107 | + } finally { |
| 108 | + t.initCause(new RuntimeException()); // Expect: State Refinement Error |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // the exception may be thrown before the check, so the catch cannot assume it |
| 113 | + static void pathConditionInCatch(int x) { |
| 114 | + try { |
| 115 | + load(); |
| 116 | + if (x <= 0) |
| 117 | + return; |
| 118 | + } catch (Exception e) { |
| 119 | + @Refinement("_ > 0") |
| 120 | + int z = x; // Expect: Refinement Error |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // after the join the catch path may have run, so the check from the try does not hold |
| 125 | + static void pathConditionAfterTry(int x) { |
| 126 | + try { |
| 127 | + load(); |
| 128 | + if (x <= 0) |
| 129 | + return; |
| 130 | + } catch (Exception e) { |
| 131 | + } |
| 132 | + @Refinement("_ > 0") |
| 133 | + int z = x; // Expect: Refinement Error |
| 134 | + } |
| 135 | + |
| 136 | + // the finally also runs on the return, so it cannot assume the check |
| 137 | + static void pathConditionInFinally(int x) { |
| 138 | + try { |
| 139 | + if (x <= 0) |
| 140 | + return; |
| 141 | + } finally { |
| 142 | + @Refinement("_ > 0") |
| 143 | + int z = x; // Expect: Refinement Error |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // the finally changes x, so the check from before the try does not hold after it |
| 148 | + static void finallyChangesCheckedVariable(int x) throws Exception { |
| 149 | + if (x <= 0) |
| 150 | + return; |
| 151 | + try { |
| 152 | + load(); |
| 153 | + } finally { |
| 154 | + x = -1; |
| 155 | + } |
| 156 | + @Refinement("_ > 0") |
| 157 | + int z = x; // Expect: Refinement Error |
| 158 | + } |
| 159 | + |
| 160 | + // the finally changes x, so the check from the try does not hold after it |
| 161 | + static void finallyChangesVariableCheckedInTry(int x) { |
| 162 | + try { |
| 163 | + if (x <= 0) |
| 164 | + return; |
| 165 | + } finally { |
| 166 | + x = -1; |
| 167 | + } |
| 168 | + @Refinement("_ > 0") |
| 169 | + int z = x; // Expect: Refinement Error |
| 170 | + } |
| 171 | +} |
0 commit comments