diff options
author | Paul Phillips <paulp@improving.org> | 2010-12-03 05:02:37 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-12-03 05:02:37 +0000 |
commit | 715d95479e77b06dd65303886d2e0e70ddcf461f (patch) | |
tree | 991f4e4be398f9b32ef6c4df49fbd927ae8dd6d6 /test/files/run/pf-catch.scala | |
parent | 31533385b7ac4e1643ad8664141f3d197dc637f1 (diff) | |
download | scala-715d95479e77b06dd65303886d2e0e70ddcf461f.tar.gz scala-715d95479e77b06dd65303886d2e0e70ddcf461f.tar.bz2 scala-715d95479e77b06dd65303886d2e0e70ddcf461f.zip |
Generalizes catch blocks to include any Partial...
Generalizes catch blocks to include any PartialFunction[Throwable, T].
Existing catch blocks will compile exactly as before. Anything else
(which mean: the token after CATCH is not a left brace, or it is a left
brace not immediately followed by CASE) is desugared as follows:
try body catch expr
// becomes
try body
catch { case x =>
val catchFn = expr
if (catchFn isDefinedAt x) catchFn(x)
else throw x
}
Review by odersky.
Diffstat (limited to 'test/files/run/pf-catch.scala')
-rw-r--r-- | test/files/run/pf-catch.scala | 34 |
1 files changed, 34 insertions, 0 deletions
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 + } +} |