Skip to content

Commit 6c9f05e

Browse files
committed
Fixes to exception handling behaviour
Corrected the ordering of exception catching and added tests to verify the correct behaviour. Previously, the universal catch would be the one on the outermost try (should be the innermost try). Also, a throw can never throw directly past this catch, so there is no need to include these alternatives (they will appear in the rethrow of the finally).
1 parent 8360233 commit 6c9f05e

File tree

11 files changed

+172
-14
lines changed

11 files changed

+172
-14
lines changed
189 Bytes
Binary file not shown.
164 Bytes
Binary file not shown.
780 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
test.class
3+
^VERIFICATION SUCCESSFUL$
4+
--
5+
^warning: ignoring
6+
--
7+
This test verifies that catches are executed in the correct order
8+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class A extends RuntimeException {}
2+
class B extends A {}
3+
4+
// This test verifies that catches are executed in the correct order
5+
public class test {
6+
public static void main (String args[]) {
7+
int i = 0;
8+
try {
9+
try {
10+
throw new A();
11+
}
12+
catch(A exc) {
13+
i++;
14+
throw new B();
15+
}
16+
finally
17+
{
18+
// Make sure A threw into the catch
19+
assert(i==1);
20+
i++;
21+
}
22+
}
23+
catch(B exc) {
24+
// Make sure finally was executed
25+
assert(i==2);
26+
i++;
27+
}
28+
// Make sure B was rethrown by finally
29+
assert(i==3);
30+
}
31+
}
189 Bytes
Binary file not shown.
164 Bytes
Binary file not shown.
1.04 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
test.class
3+
VERIFICATION SUCCESSFUL
4+
--
5+
^warning: ignoring
6+
--
7+
Tests embedded try-catch-finally to ensure the catching order is correct
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import org.cprover.CProver;
2+
class A extends RuntimeException {}
3+
class B extends A {}
4+
5+
// This test verifies that catches are executed in the correct order
6+
public class test {
7+
public static void main (String args[]) {
8+
int i = 0;
9+
int j = 0;
10+
try {
11+
try {
12+
try {
13+
if (CProver.nondetBoolean()) throw new A();
14+
else throw new B();
15+
}
16+
catch(B exc) {
17+
i++;
18+
}
19+
catch(A exc) {
20+
i++;
21+
throw new B();
22+
}
23+
finally
24+
{
25+
// Make sure A threw into the catch
26+
assert(i==1);
27+
i++;
28+
}
29+
assert(i==2);
30+
// Account for the rethrow in finally
31+
j++;
32+
}
33+
catch(B exc) {
34+
// Make sure finally was executed
35+
assert(i==2);
36+
i++;
37+
throw new B();
38+
}
39+
finally
40+
{
41+
assert ((i+j) == 3);
42+
}
43+
// Account for the rethrow in finally
44+
j++;
45+
}
46+
catch(B exc)
47+
{
48+
i++;
49+
}
50+
// Make sure B was rethrown by finally when A was thrown
51+
// or if B was thrown there was no rethrow
52+
assert(i+j == 4);
53+
}
54+
}

0 commit comments

Comments
 (0)