@throws in Scala does not allow Java to be called to catch the correct type of exception

I have a Scala code like this:

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

Calling it from Java as follows:

 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) { } } } 

Deleting a catch block results in an error from the Java compiler talking about the "unhandled exception type MyCheckedException". Adding a catch block to MyCheckedException causes the compiler to complain that the catch block is unavailable because the exception is never thrown.

If I catch an Exception and make an instanceOf, I can catch the correct exception coming from doStuff, but I thought that the @throws annotation should generate the correct bytecode for the correct catch block to work. Am I mistaken, or is there a mistake here?

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

Edit:. It compiles a fine calling javac / scalac using sbt from the command line. The error is obvious only at compile time like in Eclipse, which means that the error is present either in the Java Eclipse compiler or in some part of the IDE. Can others reproduce it like this? I am using Eclipse 3.7.2

+7
source share
2 answers

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

+6
source

For future reference, this problem has been fixed (https://github.com/scala-ide/scala-ide/commit/055a81cd3fe792e4327668791888c30cf04793f5). The fix is ​​already available using Scala IDE 2.0.x and Helium night hours . In addition, it will be included in the next release of Scala IDE 2.0.2.

(sorry for the extra noise, but I realized that the answer was more noticeable than a simple comment)

+2
source

All Articles