diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/files/run/pf-catch.check | 4 | ||||
-rw-r--r-- | test/files/run/pf-catch.scala | 34 |
2 files changed, 38 insertions, 0 deletions
diff --git a/test/files/run/pf-catch.check b/test/files/run/pf-catch.check new file mode 100644 index 0000000000..faee9566af --- /dev/null +++ b/test/files/run/pf-catch.check @@ -0,0 +1,4 @@ +NoSuchElementException +NullPointerException slipped by. +NoSuchElementException +DEBUG: NullPointerException diff --git a/test/files/run/pf-catch.scala b/test/files/run/pf-catch.scala new file mode 100644 index 0000000000..f0b8baeeb3 --- /dev/null +++ b/test/files/run/pf-catch.scala @@ -0,0 +1,34 @@ +object Test { + def shortName(x: AnyRef) = x.getClass.getName split '.' last + type Handler[+T] = PartialFunction[Throwable, T] + + val standardHandler: Handler[String] = { + case x: java.util.NoSuchElementException => shortName(x) + case x: java.lang.IllegalArgumentException => shortName(x) + } + + def fn[T: Handler](body: => T): T = { + try body + catch implicitly[Handler[T]] + } + + def f1 = { + implicit val myHandler = standardHandler + println(fn(Nil.head)) + println(fn(null.toString)) + } + def f2 = { + implicit val myHandler: Handler[String] = standardHandler orElse { + case x => "DEBUG: " + shortName(x) + } + println(fn(Nil.head)) + println(fn(null.toString)) + } + + def main(args: Array[String]): Unit = { + try f1 + catch { case x => println(shortName(x) + " slipped by.") } + + f2 + } +} |