Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@throws in Scala does not allow calling Java to catch correct exception type

I have some Scala code like this:

class Callee {
  @throws(classOf[MyCheckedException])
  def doStuff() {
  }
}

Calling it from Java like so:

public class Caller {

  public static void main(String[] args) {
    // this won't compile; the Java compiler complains that the catch block is unreachable
    // however without the catch block, it complains "unhandled exception MyCheckedException"
    try {
      new Callee().doStuff();
    }
    catch (MyCheckedException e) {

    }
  }
}

Removing the catch block results in an error from the Java compiler saying 'unhandled exception type MyCheckedException'. Adding the catch block for MyCheckedException results in the compiler complaining about the catch block being unreachable, because the exception is never thrown.

If I catch Exception and do an instanceOf, I can trap the correct exception coming out of doStuff, but I thought the @throws annotation was supposed to generate the right bytecode for the proper catch block to work. Am I wrong, or is there a bug here?

For the record, this is with Scala 2.9.2 and Java 1.6.

Edit: It compiles fine invoking javac/scalac using sbt from the command line. The error is only apparent during compile-as-you-type in Eclipse, which suggests the bug is in either the Eclipse Java Compiler or some part of the IDE. Can others reproduce it this way? I am using Eclipse 3.7.2

like image 695
David North Avatar asked Apr 26 '12 13:04

David North


1 Answers

I can reproduce this on Helios with 2.9.1. It is a bug in the presentation compiler, and you should raise it as a bug on http://www.assembla.com/spaces/scala-ide/tickets.

like image 194
Matthew Farwell Avatar answered Sep 29 '22 12:09

Matthew Farwell