2012-04-26 27 views
7

我有一些Scala代碼是這樣的:拋出:Scala中不允許調用Java趕上正確的異常類型

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

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

    } 
    } 
} 

卸下catch塊導致Java編譯器發出錯誤,說「未處理的異常類型MyCheckedException」。爲MyCheckedException添加catch塊導致編譯器抱怨catch塊無法訪問,因爲從不拋出異常。

如果我抓到Exception並執行一個instanceOf,我可以捕獲來自doStuff的正確異常,但我認爲@throws註釋應該生成適當的catch塊的正確字節碼。我錯了嗎,還是這裏有bug?

爲了記錄,這是與Scala 2.9.2和Java 1.6。

編輯:它使用sbt從命令行編譯罰款調用javac/scalac。該錯誤僅在Eclipse中以編譯即用類型的方式出現,這表明該錯誤位於Eclipse Java Compiler或IDE的某個部分。其他人可以用這種方式重現嗎?我正在使用Eclipse 3.7.2

+1

幫助我們來幫助你,表現出一定的代碼 – 2012-04-26 13:10:04

+0

無法重現,你可以提供一個完整的例子嗎? – axtavt 2012-04-26 13:46:15

+0

現在包含的全部課程 – 2012-04-26 13:50:04

回答

6

我可以在2.9.1的Helios上重現這一點。這是演示編譯器中的一個錯誤,您應該將其作爲http://www.assembla.com/spaces/scala-ide/tickets上的錯誤提升。

+2

謝謝。它可能是也可能不是http://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/[email protected]斯卡拉特質,但我現在將單獨提出 – 2012-04-26 16:13:33

+0

@David North我很確定它是您鏈接的票的複製品。也許你可以將你的測試用例添加到同一張票。 – 2012-04-27 05:45:41

+0

已完結http://scala-ide-portfolio.assembla.com/spaces/scala-ide/support/tickets/[email protected]ing -碼 – 2012-04-27 08:31:28

2

爲了將來的參考,此問題已得到解決(https://github.com/scala-ide/scala-ide/commit/055a81cd3fe792e4327668791888c30cf04793f5)。該修補程序已在Scala IDE 2.0.x and Helium nightlies中提供。此外,它將包含在下一個Scala IDE 2.0.2維護版本中。

(抱歉額外的噪聲,但我意識到,有一個答案是不是簡單的評論更可見)