aboutsummaryrefslogtreecommitdiff
path: root/tests/run/exceptions-nest.scala
blob: 87586485c0c97f27399a269d44e6278af450cbd2 (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
object Test extends dotty.runtime.LegacyApp {

  println(test1)
  println(test2)
  println(test3)
  println(test4)
  println(test5)
  try { println(test6) } catch { case _: Throwable => println("OK") }
  println(test7)
  try { println(test8) } catch { case _: Throwable => println("OK") }
  println(test9)
  println(test10)
  println(test11)
  println(test12)

  def test1 = {
    var x = 1
    try {
      x = 2
    } catch {
      case _: NullPointerException => x = 3
      case _: Throwable => x = 4
    }
    x
  }

  def test2 = {
    var x = 1
    try {
      x = 2
      try {
        x = 21
      } catch {
        case _: Throwable => x = 22
      }
      x = 23
    } catch {
      case _: NullPointerException => x = 3
      case _: Throwable => x = 4
    }
    x
  }

  def test3 = {
    var x = 1
    try {
      try{x = 2} catch { case _: Throwable => x = 4 }
    } catch {
      case _: NullPointerException => x = 3
      case _: Throwable => x = 4
    }
    x
  }

  def test4 = {
    var x = 1
    try {
      x = 2
    } catch {
      case _: NullPointerException => x = 3
      case _: Throwable => x = 4
    }
    try {
      x = 5
    } catch {
      case _: NullPointerException => x = 6
    }
    x
  }

  def test5 = {
    var x = 1
    try {
      x = 2
    } catch {
      case _: NullPointerException => try { x = 3 } catch { case f: Throwable => throw f }
      case _: Throwable => x = 4; try { x = 41 } catch { case _: Exception => x = 42 }; x = 43
    }
    x
  }

  def test6: Int = {
    var x = 1
    try {
      x = 2
      (null: String).toString
    } catch {
      case e: NullPointerException =>
        throw e
      case _: Throwable =>
        x = 3
        return 1000
    } finally {
      x = 4
      println(x)
    }
    x
  }

  def test7 = {
    var x = 1
    try {
      x = 2
    } finally {
      try {
        x = 4
      } catch {
        case _: Throwable => x = 5
      }
    }
    x
  }

  def test8 = {
    var x = 1
    try {
      throw new NullPointerException
    } catch {
      case e: Throwable => throw e
    }
    x
  }

  def test9 = {
    try { "" match {
      case s: String => 10
    }} catch { case _: Throwable => 20 }
  }

  var x10 = 1
  def test10: Int = {
    try { 1 }
    catch { case e if (x10 == 1) => 1 }
  }

   def test11: Unit = {
    try { () }
    catch { case e: Throwable => () }
  }

  class E1 extends Exception
  class E2 extends Exception
  class E3 extends Exception

  def test12_impl(op: => Int) = try {
    op
  } catch {
    case e: E1 => 2
    case e: E2 => 3
    case e: E3 => 4
  }
  def test12 =
    test12_impl(1) +
    test12_impl(throw new E1) +
    test12_impl(throw new E2) +
    test12_impl(throw new E3)
}