@@ -7,53 +7,47 @@ final class CatchThrowableTest extends AnyFunSuite with AnalyzerTest {
77 test(" catching Throwable should be rejected" ) {
88 assertErrors(1 ,
99 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+ |}
1615 | """ .stripMargin)
1716 }
1817
1918 test(" catching specific exceptions should be allowed" ) {
2019 assertNoErrors(
2120 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+ |}
3028 | """ .stripMargin)
3129 }
3230
3331 test(" catching Throwable with other exceptions should be rejected" ) {
3432 assertErrors(1 ,
3533 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)
4339 |}
4440 | """ .stripMargin)
4541 }
4642
4743 test(" catching Throwable in nested catch block should be rejected" ) {
4844 assertErrors(1 ,
4945 scala """
50- |def test(): Unit = {
51- | try println("test")
46+ |try println("test")
47+ |catch {
48+ | case e: Exception => try println("test")
5249 | catch {
53- | case e: Exception => try println("test")
54- | catch {
55- | case e: Throwable => println(e)
56- | }
50+ | case e: Throwable => println(e)
5751 | }
5852 |}
5953 | """ .stripMargin)
@@ -70,46 +64,55 @@ final class CatchThrowableTest extends AnyFunSuite with AnalyzerTest {
7064 | case _ => None
7165 | }
7266 |}
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)
8174 |}
8275 | """ .stripMargin)
8376 }
8477
8578 test(" catching non-Throwable with pattern match should be allowed" ) {
8679 assertNoErrors(
8780 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!")
9985 |}
100- | """ .stripMargin)
86+ |try {
87+ | println("test")
88+ |} catch {
89+ | case e@(_: IndexOutOfBoundsException | _: NullPointerException) => println("OK!")
90+ |}
91+ | """ .stripMargin
92+ )
10193 }
10294
10395 test(" catching Throwable with pattern match should be rejected" ) {
10496 assertErrors(1 ,
10597 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!")
112102 |}
113103 | """ .stripMargin)
114104 }
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+ }
115118}
0 commit comments