File: exceptions.nice

package info (click to toggle)
nice 0.9.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,220 kB
  • ctags: 6,894
  • sloc: java: 42,767; xml: 3,508; lisp: 1,079; sh: 736; makefile: 673; cpp: 21; awk: 3
file content (111 lines) | stat: -rw-r--r-- 1,757 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Tests for exception handling

package regtest.basic;

class MyEx extends Exception {
  String info;
}

void test_exceptions();
test_exceptions()
{
  println("\n### Testing exceptions ###");
  ?String s = null;
  try{
    try{
      notNull(s).length();
    }
    finally{
      println("finally");
    }
  }
  catch(NullPointerException e){
    println("NullPointerException");
  }
  catch(nice.lang.AssertionFailed e){
    println("NullPointerException");
  }
  
  try{
    MyEx ex = new MyEx(info:"woula");
    throw ex;
  }
  catch(MyEx e){
    println("MyEx: info=" + e.info);
  }
  catch(Exception e){
    println("Exception");
  }

  try{
    exnRaiser(false);

    boolean b = s == null;
    if (b) 
      throw new MyEx(info: "");
    else
      return;
  }
  catch (MyEx e) {
  }

  try {
    // This throw expression is well-typed
    // It will never be executed but should be compiled into valid bytecode.
    throw neverReturns();
  }
  catch(Error e) {
  }

  testFinally(true);
  testFinally(false);  
  testFinallyInt(true);
  testFinallyInt(false);  
}

<T> T neverReturns()
{
  throw new Error();
}

int exnRaiser(boolean b) 
{
  if (b)
    throw new MyEx(info: "");
  else
    return 1;
}

void testFinally(boolean callReturn)
{
  try{
    try{
      if (callReturn)
	return;
    }
    finally{
      println("Finally is executed if return is called from try");
    }
    return;
  }
  finally{
    println("Outer finally blocks must be executed too");
  }
}

int testFinallyInt(boolean callReturn)
{
  try{
    try{
      if (callReturn)
	return 0;
    }
    finally{
      println("Finally is executed if return is called from try");
    }
    return 1;
  }
  finally{
    println("Outer finally blocks must be executed too");
  }
}