Scala中的@throws不允许调用Java来捕获正确的异常类型

时间:2022-03-21 22:35:10

I have some Scala code like this:

我有一些像这样的Scala代码:

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

Calling it from Java like so:

像这样从Java调用它:

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.

删除catch块会导致Java编译器发出错误,指出“未处理的异常类型MyCheckedException”。为MyCheckedException添加catch块会导致编译器抱怨catch块无法访问,因为永远不会抛出异常。

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?

如果我捕获Exception并执行一个instanceOf,我可以捕获doStuff中出现的正确异常,但我认为@throws注释应该为正确的catch块生成正确的字节码。我错了,或者这里有错误吗?

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

对于记录,这是使用Scala 2.9.2和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

编辑:它使用命令行中的sbt编译精细调用javac / scalac。这个错误只在Eclipse中编译为你的类型时显而易见,这表明该错误存在于Eclipse Java编译器或IDE的某些部分中。其他人可以这样重现吗?我正在使用Eclipse 3.7.2

2 个解决方案

#1


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.

我可以使用2.9.1在Helios上重现这一点。这是演示编译器中的一个错误,您应该将其作为http://www.assembla.com/spaces/scala-ide/tickets上的错误提出。

#2


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.

为了将来参考,此问题已得到修复(https://github.com/scala-ide/scala-ide/commit/055a81cd3fe792e4327668791888c30cf04793f5)。 Scala IDE 2.0.x和Helium nightlies已经提供了此修复程序。此外,它将包含在下一个Scala IDE 2.0.2维护版本中。

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

(对不起额外的噪音,但我意识到答案比简单评论更明显)

#1


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.

我可以使用2.9.1在Helios上重现这一点。这是演示编译器中的一个错误,您应该将其作为http://www.assembla.com/spaces/scala-ide/tickets上的错误提出。

#2


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.

为了将来参考,此问题已得到修复(https://github.com/scala-ide/scala-ide/commit/055a81cd3fe792e4327668791888c30cf04793f5)。 Scala IDE 2.0.x和Helium nightlies已经提供了此修复程序。此外,它将包含在下一个Scala IDE 2.0.2维护版本中。

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

(对不起额外的噪音,但我意识到答案比简单评论更明显)