Skip to content

Commit 66be6e1

Browse files
author
mdodsworth
committed
adding exeception handling
1 parent cbeda55 commit 66be6e1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

exception-passing.scala

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def exceptionToLeft[T](f: => T): Either[Exception, T] = {
2+
try {
3+
Right(f)
4+
}
5+
catch {
6+
case e:Exception => Left(e)
7+
}
8+
}
9+
10+
def testExceptionThrower(i:Int) = {
11+
i % 2 match {
12+
case 0 => throw new RuntimeException("boom!")
13+
case 1 => i
14+
}
15+
}
16+
17+
for(i <- 1 to 3) {
18+
exceptionToLeft(testExceptionThrower(i)) match {
19+
case Left(ex) => println("pop!")
20+
case Right(x) => println(x)
21+
}
22+
}
23+

0 commit comments

Comments
 (0)