Aladdin - Scala Bugtracking
[#484] project: nsc priority: low category: bug
submitter assigned to status date submitted
Iulian Iulian fixed 2005-11-10 16:56:15.0
subject try catch blocks inside expressions generate invalid code
code
object Foo extends AnyRef with Application {
  val x = 1;

  Console.print("1 + 1 = ");
  Console.println(1 + (try {
    x;
  } catch {
    case _: Error => 5;
  }))
}
what happened
Bytecode will fail verification. The reason is the way JVM handles the stack when exceptions occur: it clears th\
e stack, pushes the exception object and transfers control to the appropriate handler. This way, all intermediat\
e values found on the operand stack are lost (in our example, it is the Console.MODULE$ instance and the first '\
1' in the addition). When the exception handler transfers control back to the instructions after the try-catch b\
lock, the stack will contain only the result value in the exception handler (here, 5), while normal control flow\
 will contain the object and the int.
what expected Correct compilation. This can be solved either through a transformation on source code to 'lift' try-catch blocks outside contexts that depend on previous results (Apply nodes or Assignment to fields, etc.), or through stack dumping: before entering a Try block, all operands on the stack are saved to local variables, and each exception handler restores the stack before handling the exception.
[back to overview]
Changes of this bug report
Iulian  edited on  2005-11-10 16:57:26.0
Iulian  edited on  2005-11-16 17:41:45.0