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

2

7

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

Norean answered 26/4, 2012 at 13:5 Comment(9)
help us to help you ,show some codeMeredith
Cannot reproduce, can you provide a complete example?Namnama
Full classes now included aboveNorean
Still cannot reproduce on javac 1.6.0_24, scalac 2.9.2. How do you compile it?Namnama
See above - the problem is only apparent during compile-as-you-type from Eclipse. I had assumed that javac on the command line would match this, but I was wrong.Norean
@DavidNorth Have you attempted to do a Clean and Rebuild on the project? Sometimes that appeases Eclipse.Prince
Clean and Rebuild doesn't help here. Nor does changing the build order in the scala workspace parameters.Actinia
@MatthewFarwell You are correct, I just tried it myself. I think you're right its a bug.Prince
For future reference, this issue has been fixed (github.com/scala-ide/scala-ide/commit/…). The fix is already available with both Scala IDE 2.0.x and Helium nightlies. Furthermore, it will be included in the next Scala IDE 2.0.2 maintenace release.Elute
A
6

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.

Actinia answered 26/4, 2012 at 14:59 Comment(3)
Thanks. It may or may not be a duplicate of scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/…, but I'll raise it separately for nowNorean
@David North I'm pretty sure it is a duplicate of the ticket you linked. Maybe you can add your test case to the same ticket.Elute
Ended up raising scala-ide-portfolio.assembla.com/spaces/scala-ide/support/…Norean
E
2

For future reference, this issue has been fixed (https://github.com/scala-ide/scala-ide/commit/055a81cd3fe792e4327668791888c30cf04793f5). The fix is already available with both Scala IDE 2.0.x and Helium nightlies. Furthermore, it will be included in the next Scala IDE 2.0.2 maintenace release.

(sorry for the additional noise, but I realized that having an answer was more visible than a simple comment)

Elute answered 17/5, 2012 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.