Line | |
---|
1 | package testcode;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Tests try/catch
|
---|
5 | */
|
---|
6 | public class TryCatch {
|
---|
7 |
|
---|
8 | public TryCatch() {
|
---|
9 | test1();
|
---|
10 | test2();
|
---|
11 | test3();
|
---|
12 | test4();
|
---|
13 | }
|
---|
14 |
|
---|
15 | /**
|
---|
16 | *
|
---|
17 | * @param args the arguments of the call.
|
---|
18 | */
|
---|
19 | static public void main(String[] args) {
|
---|
20 | new TryCatch();
|
---|
21 | }
|
---|
22 |
|
---|
23 | public void test1() {
|
---|
24 | try {
|
---|
25 | int x = 1 / 0;
|
---|
26 | throw new IllegalStateException("div0 did not throw?!");
|
---|
27 | } catch (ArithmeticException e) {
|
---|
28 | System.out.println("ok1");
|
---|
29 | }
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void test2() {
|
---|
34 | try {
|
---|
35 | /* if stmt is to trick java compiler into accepting
|
---|
36 | * the code after the throw,
|
---|
37 | * which is safety net for testing the python translation.
|
---|
38 | */
|
---|
39 | if (true)
|
---|
40 | throw new ArithmeticException("test");
|
---|
41 | throw new IllegalStateException("throw phrase did not throw?!");
|
---|
42 | } catch (ArithmeticException e) {
|
---|
43 | System.out.println("ok2");
|
---|
44 | }
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void test4() {
|
---|
49 | try {
|
---|
50 | dothrow();
|
---|
51 | throw new IllegalStateException("test4 did not throw?!");
|
---|
52 | } catch (NullPointerException | ArithmeticException e) {
|
---|
53 | System.out.println("ok4");
|
---|
54 | }
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void test3() {
|
---|
59 | try {
|
---|
60 | dothrow();
|
---|
61 | System.out.println("throw phrase did not throw?!");
|
---|
62 | return;
|
---|
63 | } catch (Exception e) {
|
---|
64 | System.out.println("ok3");
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void dothrow() {
|
---|
70 | throw new ArithmeticException("test");
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.