File tree Expand file tree Collapse file tree 1 file changed +22
-8
lines changed Expand file tree Collapse file tree 1 file changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -17,15 +17,29 @@ def foo(x: Boolean): Int throws Fail =
17
17
def bar (x : Boolean )(using CanThrow [Fail ]): Int = foo(x)
18
18
def baz : Int throws Exception = foo(false )
19
19
20
- @ main def Test =
20
+ class Result [T ]:
21
+ var value : T = scala.compiletime.uninitialized
22
+
23
+ def tryCatch [R , E <: Exception ](body : => R throws E )(c : E => Unit , f : => Unit = ()): R =
24
+ val res = new Result [R ]
21
25
try
22
- given CanThrow [Fail ] = ???
26
+ given CanThrow [E ] = ???
27
+ res.value = body
28
+ catch c.asInstanceOf [Throwable => Unit ]
29
+ finally f
30
+ res.value
31
+
32
+ @ main def Test =
33
+ tryCatch({
23
34
println(foo(true ))
24
35
println(foo(false ))
25
- catch case ex : Fail =>
26
- println(" failed" )
27
- try
28
- given CanThrow [Exception ] = ???
36
+ })({
37
+ case ex : Fail =>
38
+ println(" failed" )
39
+ })
40
+ tryCatch(
29
41
println(baz)
30
- catch case ex : Fail =>
31
- println(" failed" )
42
+ )({
43
+ case ex : Fail =>
44
+ println(" failed" )
45
+ })
You can’t perform that action at this time.
0 commit comments