From 3ebac4319b2ec21de7de1c3d228c370ea0e4675b Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Mon, 30 Jan 2006 10:15:18 +0000 Subject: Added more test files for try-catch blocks. --- test/files/run/exceptions-2.check | 31 +++++++ test/files/run/exceptions-2.scala | 164 ++++++++++++++++++++++++++++++++++++++ test/files/run/try-2.check | 4 + test/files/run/try-2.scala | 62 ++++++++++++++ 4 files changed, 261 insertions(+) create mode 100644 test/files/run/exceptions-2.check create mode 100644 test/files/run/exceptions-2.scala create mode 100644 test/files/run/try-2.check create mode 100644 test/files/run/try-2.scala (limited to 'test/files') diff --git a/test/files/run/exceptions-2.check b/test/files/run/exceptions-2.check new file mode 100644 index 0000000000..40edd78ca2 --- /dev/null +++ b/test/files/run/exceptions-2.check @@ -0,0 +1,31 @@ +nested1: +Innermost finally +Outermost finally +nested2: +Innermost finally +Outermost finally +mixed: +Finally! +10 +withValue1: +Oh, oh +10 +method2: +10 +Exception occurred +method3: +Exception occurred with stack trace: +java.lang.NullPointerException + at Test$.method3(exceptions-2.scala:108) + at Test$$anonfun5.apply(exceptions-2.scala:151) + at Test$$anonfun5.apply(exceptions-2.scala:151) + at Test$.execute(exceptions-2.scala:128) + at Test$.main(exceptions-2.scala:151) + at Test.main(exceptions-2.scala) +================= +NoExcep.method2: +Hello, world +NoExcep.method3: +method3 +NoExcep.method4: +.. diff --git a/test/files/run/exceptions-2.scala b/test/files/run/exceptions-2.scala new file mode 100644 index 0000000000..2583542747 --- /dev/null +++ b/test/files/run/exceptions-2.scala @@ -0,0 +1,164 @@ +/* + * Try exception handling and finally blocks. + * + * $Id: $ + */ + + +trait Tree extends Exception; + +case class Node(a: Tree, b: Tree) extends Tree; +case class Leaf(x: Int) extends Tree; + + +object NoExcep { + def a = true; + def b = false; + def c = true; + + def method1(t: Tree) = try { + Console.println(t); + } catch { + case Node(Leaf(_), Leaf(_)) => a; + case Leaf(_) => b; + } + + def method2 = try { + Console.println("Hello, world"); + } catch { + case _: Error => Console.println("File error"); + case t: Throwable => Console.println("Unknown error"); + } + + def method3 = try { + try { + Console.println("method3"); + } catch { + case Node(Leaf(_), Leaf(_)) => Console.println("First one"); + case Leaf(_) => Console.println("Second one"); + } + } catch { + case _: Error => Console.println("File error"); + case t: Exception => Console.println("Unknown error"); + } + + def method4 = try { + Console.println(".."); + } catch { + case _ => error(".."); + } +} + +object Test { + def nested1: Unit = try { + try { + error("nnnnoooo"); + } finally { + Console.println("Innermost finally"); + } + } finally { + Console.println("Outermost finally"); + } + + def nested2 = try { + try { + error("nnnnoooo"); + } finally { + Console.println("Innermost finally"); + } + Console.println("Intermediary step"); + } finally { + Console.println("Outermost finally"); + } + + def mixed = + try { + if (10 > 0) + throw Leaf(10); + Console.println("nooo oneeee can priiiint meee"); + } catch { + case Leaf(a) => Console.println(a); + case _: Exception => Console.println("Exception occurred"); + } finally { + Console.println("Finally!"); + } + + def method2: Unit = { + try { + if (10 > 0) + throw Leaf(10); + Console.println("nooo oneeee can priiiint meee"); + } catch { + case Leaf(a) => Console.println(a); + case _: Exception => Console.println("Exception occurred"); + } + + try { + val a: Leaf = null; + a.x; + } catch { + case Leaf(a) => Console.println(a); + case _: NullPointerException => Console.println("Exception occurred"); + } + } + + def method3: Unit = try { + try { + val a: Leaf = null; + a.x; + } catch { + case Leaf(a) => Console.println(a); + } + } catch { + case npe: NullPointerException => + Console.println("Exception occurred with stack trace:"); + npe.printStackTrace(); + } + + def withValue1: Unit = { + val x = try { + 10 + } finally { + Console.println("Oh, oh"); + }; + Console.println(x); + } + + def execute(f: => Unit) = try { + f; + } catch { + case _ => (); + } + + + def main(args: Array[String]): Unit = { + Console.println("nested1: "); + execute(nested1); + + Console.println("nested2: "); + execute(nested2); + + Console.println("mixed: "); + execute(mixed); + + Console.println("withValue1:"); + execute(withValue1); + + Console.println("method2:"); + execute(method2); + + Console.println("method3:"); + execute(method3); + + Console.println("================="); + + Console.println("NoExcep.method2:"); + execute(NoExcep.method2); + + Console.println("NoExcep.method3:"); + execute(NoExcep.method3); + + Console.println("NoExcep.method4:"); + execute(NoExcep.method4); + } +} diff --git a/test/files/run/try-2.check b/test/files/run/try-2.check new file mode 100644 index 0000000000..6c4a024c93 --- /dev/null +++ b/test/files/run/try-2.check @@ -0,0 +1,4 @@ +exception happened + +Nothin +Nothin diff --git a/test/files/run/try-2.scala b/test/files/run/try-2.scala new file mode 100644 index 0000000000..747f81e0ce --- /dev/null +++ b/test/files/run/try-2.scala @@ -0,0 +1,62 @@ +/* + * Test different variants of the try-catch block. + * + * $Id: $ + */ + + +object Test { + + + def tryAllUnit: Unit = + try { + throw new Error(); + } + catch { + case _ => Console.println("exception happened\n"); + } + + def tryUnitAll: Unit = + try { + Console.println("Nothin"); + } catch { + case _ => error("Bad, bad, lama!"); + } + + def tryAllAll: Unit = + try { + throw new Error(); + } catch { + case _ => error("Bad, bad, lama!"); + } + + def tryUnitUnit: Unit = + try { + Console.println("Nothin"); + } catch { + case _ => Console.println("Nothin"); + } + + def tryIntUnit: Unit = + try { + 10; + } catch { + case _ => Console.println("Huh?"); + } + + + def execute(f: => Unit) = try { + f; + } catch { + case _ => (); + } + + + def main(args:Array[String]): Unit = { + execute(tryAllUnit); + execute(tryUnitAll); + execute(tryAllAll); + execute(tryUnitUnit); + execute(tryIntUnit); + } +} -- cgit v1.2.3