summaryrefslogtreecommitdiff
path: root/test/files/run/finally.scala
diff options
context:
space:
mode:
authorIulian Dragos <jaguarul@gmail.com>2011-03-04 16:10:40 +0000
committerIulian Dragos <jaguarul@gmail.com>2011-03-04 16:10:40 +0000
commit38adb1426f2d06a6993d3cc579db76efcd92a0ec (patch)
tree029d0d2e4dff52d04c0601a5ce837e753f7f64cf /test/files/run/finally.scala
parente586206e08e70b22d33152c7802d658577fddcf5 (diff)
downloadscala-38adb1426f2d06a6993d3cc579db76efcd92a0ec.tar.gz
scala-38adb1426f2d06a6993d3cc579db76efcd92a0ec.tar.bz2
scala-38adb1426f2d06a6993d3cc579db76efcd92a0ec.zip
Fixed several issues with finally, closes #3965.
Diffstat (limited to 'test/files/run/finally.scala')
-rw-r--r--test/files/run/finally.scala107
1 files changed, 100 insertions, 7 deletions
diff --git a/test/files/run/finally.scala b/test/files/run/finally.scala
index 0da616cfdd..b3b7f684e5 100644
--- a/test/files/run/finally.scala
+++ b/test/files/run/finally.scala
@@ -1,6 +1,17 @@
-// test that finally is not covered by any exception handlers.
object Test extends App {
+
+
+ // test that finally is not covered by any exception handlers.
+ def throwCatchFinally {
+ try {
+ bar
+ } catch {
+ case e => println(e)
+ }
+ }
+
+ // test that finally is not covered by any exception handlers.
def bar {
try {
println("hi")
@@ -14,7 +25,8 @@ object Test extends App {
}
}
- def m1 {
+ // return in catch (finally is executed)
+ def retCatch {
try {
throw new Exception
} catch {
@@ -24,11 +36,92 @@ object Test extends App {
} finally println("in finally")
}
- try {
- bar
- } catch {
- case e => println(e)
+ // throw in catch (finally is executed, exception propagated)
+ def throwCatch {
+ try {
+ throw new Exception
+ } catch {
+ case e =>
+ println(e);
+ throw e
+ } finally println("in finally")
+ }
+
+ // return inside body (finally is executed)
+ def retBody {
+ try {
+ return
+ } catch {
+ case e =>
+ println(e);
+ throw e
+ } finally println("in finally")
+ }
+
+ // throw inside body (finally and catch are executed)
+ def throwBody {
+ try {
+ throw new Exception
+ } catch {
+ case e =>
+ println(e);
+ } finally println("in finally")
+ }
+
+ // return inside finally (each finally is executed once)
+ def retFinally {
+ try {
+ try println("body")
+ finally {
+ println("in finally 1")
+ return
+ }
+ } finally println("in finally 2")
+ }
+
+
+ // throw inside finally (finally is executed once, exception is propagated)
+ def throwFinally {
+ try {
+ try println("body")
+ finally {
+ println("in finally")
+ throw new Exception
+ }
+ } catch {
+ case e => println(e)
+ }
+ }
+
+ // nested finallies with return value
+ def nestedFinalies: Int =
+ try {
+ try {
+ return 10
+ } finally {
+ try { () } catch { case _ => () }
+ println("in finally 1")
+ }
+ } finally {
+ println("in finally 2")
+ }
+
+ def test[A](m: => A, name: String) {
+ println("Running %s".format(name))
+ try {
+ m
+ } catch {
+ case e => println("COUGHT: " + e)
+ }
+ println("-" * 40)
}
- m1
+ test(throwCatchFinally, "throwCatchFinally")
+ test(retCatch, "retCatch")
+ test(throwCatch, "throwCatch")
+ test(retBody, "retBody")
+ test(throwBody, "throwBody")
+ test(retFinally, "retFinally")
+ test(throwFinally, "throwFinally")
+ test(nestedFinalies, "nestedFinalies")
}