@@ -7,53 +7,47 @@ final class CatchThrowableTest extends AnyFunSuite with AnalyzerTest {
7
7
test(" catching Throwable should be rejected" ) {
8
8
assertErrors(1 ,
9
9
scala """
10
- |def test(): Unit =
11
- | try {
12
- | println("test")
13
- | } catch {
14
- | case t: Throwable => println(t)
15
- | }
10
+ |try {
11
+ | println("test")
12
+ |} catch {
13
+ | case t: Throwable => println(t)
14
+ |}
16
15
| """ .stripMargin)
17
16
}
18
17
19
18
test(" catching specific exceptions should be allowed" ) {
20
19
assertNoErrors(
21
20
scala """
22
- |def test(): Unit =
23
- | try {
24
- | println("test")
25
- | } catch {
26
- | case e: Exception => println(e)
27
- | case e: RuntimeException => println(e)
28
- | case e: IllegalArgumentException => println(e)
29
- | }
21
+ |try {
22
+ | println("test")
23
+ |} catch {
24
+ | case e: Exception => println(e)
25
+ | case e: RuntimeException => println(e)
26
+ | case e: IllegalArgumentException => println(e)
27
+ |}
30
28
| """ .stripMargin)
31
29
}
32
30
33
31
test(" catching Throwable with other exceptions should be rejected" ) {
34
32
assertErrors(1 ,
35
33
scala """
36
- |def test(): Unit = {
37
- | try {
38
- | println("test")
39
- | } catch {
40
- | case e: IllegalArgumentException => println(e)
41
- | case t: Throwable => println(t)
42
- | }
34
+ |try {
35
+ | println("test")
36
+ |} catch {
37
+ | case e: IllegalArgumentException => println(e)
38
+ | case t: Throwable => println(t)
43
39
|}
44
40
| """ .stripMargin)
45
41
}
46
42
47
43
test(" catching Throwable in nested catch block should be rejected" ) {
48
44
assertErrors(1 ,
49
45
scala """
50
- |def test(): Unit = {
51
- | try println("test")
46
+ |try println("test")
47
+ |catch {
48
+ | case e: Exception => try println("test")
52
49
| catch {
53
- | case e: Exception => try println("test")
54
- | catch {
55
- | case e: Throwable => println(e)
56
- | }
50
+ | case e: Throwable => println(e)
57
51
| }
58
52
|}
59
53
| """ .stripMargin)
@@ -70,46 +64,55 @@ final class CatchThrowableTest extends AnyFunSuite with AnalyzerTest {
70
64
| case _ => None
71
65
| }
72
66
|}
73
- |def test(): Unit = {
74
- | try {
75
- | println("test")
76
- | } catch {
77
- | case custom(t) => println(t)
78
- | case NonFatal(t) => println(t)
79
- | case scala.util.control.NonFatal(t) => println(t)
80
- | }
67
+ |
68
+ |try {
69
+ | println("test")
70
+ |} catch {
71
+ | case custom(t) => println(t)
72
+ | case NonFatal(t) => println(t)
73
+ | case scala.util.control.NonFatal(t) => println(t)
81
74
|}
82
75
| """ .stripMargin)
83
76
}
84
77
85
78
test(" catching non-Throwable with pattern match should be allowed" ) {
86
79
assertNoErrors(
87
80
scala """
88
- |def test(): Unit = {
89
- | try {
90
- | println("test")
91
- | } catch {
92
- | case _: IndexOutOfBoundsException | _: NullPointerException => println("OK!")
93
- | }
94
- | try {
95
- | println("test")
96
- | } catch {
97
- | case e@(_: IndexOutOfBoundsException | _: NullPointerException) => println("OK!")
98
- | }
81
+ |try {
82
+ | println("test")
83
+ |} catch {
84
+ | case _: IndexOutOfBoundsException | _: NullPointerException => println("OK!")
99
85
|}
100
- | """ .stripMargin)
86
+ |try {
87
+ | println("test")
88
+ |} catch {
89
+ | case e@(_: IndexOutOfBoundsException | _: NullPointerException) => println("OK!")
90
+ |}
91
+ | """ .stripMargin
92
+ )
101
93
}
102
94
103
95
test(" catching Throwable with pattern match should be rejected" ) {
104
96
assertErrors(1 ,
105
97
scala """
106
- |def test(): Unit = {
107
- | try {
108
- | println("test")
109
- | } catch {
110
- | case _: IndexOutOfBoundsException | _: Throwable => println("Not OK!")
111
- | }
98
+ |try {
99
+ | println("test")
100
+ |} catch {
101
+ | case _: IndexOutOfBoundsException | _: Throwable => println("Not OK!")
112
102
|}
113
103
| """ .stripMargin)
114
104
}
105
+
106
+ test(" catching Throwable using custom handler should be allowed" ) {
107
+ assertNoErrors(
108
+ scala """
109
+ |object CustomHandler {
110
+ | def apply[T](): PartialFunction[Throwable, T] = ???
111
+ |}
112
+ |
113
+ |try {
114
+ | println("test")
115
+ |} catch CustomHandler()
116
+ | """ .stripMargin)
117
+ }
115
118
}
0 commit comments