summaryrefslogtreecommitdiff
path: root/test/files/run/exceptions-2.scala
blob: 739178993725879fb6bfe3465fa5ee735bd69aa0 (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Try exception handling and finally blocks.
 *
 * $Id$
 */


trait Tree extends Exception;

case class Node(a: Tree, b: Tree) extends Tree;
case class Leaf(x: Int) extends Tree;


object NoExcep {
  def a = true;
  def b = false;
  def c = true;

  def method1(t: Tree) = try {
    Console.println(t);
  } catch {
    case Node(Leaf(_), Leaf(_)) => a;
    case Leaf(_) => b;
  }

  def method2 = try {
    Console.println("Hello, world");
  } catch {
    case _: Error => Console.println("File error");
    case t: Throwable => Console.println("Unknown error");
  }

  def method3 = try {
    try {
      Console.println("method3");
    } catch {
      case Node(Leaf(_), Leaf(_)) => Console.println("First one");
      case Leaf(_) => Console.println("Second one");
    }
  } catch {
    case _: Error => Console.println("File error");
    case t: Exception => Console.println("Unknown error");
  }

  def method4 = try {
    Console.println("..");
  } catch {
    case _ => error("..");
  }
}

object Test {
  def nested1: Unit = try {
    try {
      error("nnnnoooo");
    } finally {
      Console.println("Innermost finally");
    }
  } finally {
    Console.println("Outermost finally");
  }

  def nested2 =  try {
    try {
      error("nnnnoooo");
    } finally {
      Console.println("Innermost finally");
    }
    Console.println("Intermediary step");
  } finally {
    Console.println("Outermost finally");
  }

  def mixed =
    try {
      if (10 > 0)
        throw Leaf(10);
      Console.println("nooo oneeee can priiiint meee");
    } catch {
      case Leaf(a) => Console.println(a);
      case _: Exception => Console.println("Exception occurred");
    } finally {
      Console.println("Finally!");
    }

  def method2: Unit = {
    try {
      if (10 > 0)
        throw Leaf(10);
      Console.println("nooo oneeee can priiiint meee");
    } catch {
      case Leaf(a) => Console.println(a);
      case _: Exception => Console.println("Exception occurred");
    }

    try {
      val a: Leaf = null;
      a.x;
    } catch {
      case Leaf(a) => Console.println(a);
      case _: NullPointerException => Console.println("Exception occurred");
    }
  }

  def method3: Unit = try {
    try {
      val a: Leaf = null;
      a.x;
    } catch {
      case Leaf(a) => Console.println(a);
    }
  } catch {
    case npe: NullPointerException =>
      Console.println("Cought an NPE");
  }

  def withValue1: Unit = {
    val x = try {
      10
    } finally {
      Console.println("Oh, oh");
    };
    Console.println(x);
  }

  def tryFinallyTry: Unit = {
    try {
      ()
    } finally {
      try {
        error("a");
      } catch {
        case _ => Console.println("Silently ignore exception in finally");
      }
    }
  }

  def valInFinally: Unit =
    try {
    } finally {
      val fin = "Abc";
      Console.println(fin);
    };

  def tryAndValInFinally: Unit =
    try {
    } finally {
      val fin = "Abc";
      try {
        Console.println(fin);
      } catch { case _ => () }
    };

  def returnInBody: Unit = try {
    try {
      Console.println("Normal execution...");
      return
      Console.println("non reachable code");
    } finally {
      Console.println("inner finally");
    }
  } finally {
    Console.println("Outer finally");
  }

  def returnInBodySynch: Unit = try {
    synchronized {
      try {
        Console.println("Synchronized normal execution...");
        return
        Console.println("non reachable code");
      } finally {
        Console.println("inner finally");
      }
    }
  } finally {
    Console.println("Outer finally");
  }


  def execute(f: => Unit) = try {
    f;
  } catch {
    case _ => ();
  }


  def main(args: Array[String]): Unit = {
    Console.println("nested1: ");
    execute(nested1);

    Console.println("nested2: ");
    execute(nested2);

    Console.println("mixed: ");
    execute(mixed);

    Console.println("withValue1:");
    execute(withValue1);

    Console.println("method2:");
    execute(method2);

    Console.println("method3:");
    execute(method3);

    Console.println("tryFinallyTry:");
    execute(tryFinallyTry);

    Console.println("valInFinally:");
    execute(valInFinally);
    Console.println("tryAndValInFinally");
    execute(tryAndValInFinally);

    Console.println("=================");

    Console.println("NoExcep.method2:");
    execute(NoExcep.method2);

    Console.println("NoExcep.method3:");
    execute(NoExcep.method3);

    Console.println("NoExcep.method4:");
    execute(NoExcep.method4);

    Console.println("Return inside body:");
    execute(returnInBody);

    Console.println("Return inside synchronized body:");
    execute(returnInBodySynch);
  }
}