`
alanwu
  • 浏览: 197769 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

囧,Scala2.7.x上发现一个隐蔽的大bug, finally的异常被catch回去了。。。

阅读更多

不好意思关于Scala的第一篇就是关于Scala的bug。。。

 

 

Java中异常捕获:

class OE {
public static void main(String args[]) {
try {
System.out.println("hi");
}
catch (Exception e) {
System.out.println("GOT HERE");
}
finally {
throw new RuntimeException("ouch");
}
}
}
 

 

输出:

hi
Exception in thread "main" java.lang.RuntimeException: ouch
at OE.main(OE.java:11)
 

 

Scala中的异常捕获:

try {
 println("hi")
}
catch {
 case e => println("GOT HERE")
}
finally {
 println("in finally")
 throw new RuntimeException("ouch")
}

 输出:

 

hi
in finally
GOT HERE
in finally
in finally
java.lang.RuntimeException: ouch
       at Test$.main(finally.scala:11)
       at Test.main(finally.scala)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:616)
       at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
       at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
       at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
       at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154)
       at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala

 

finally 打印后抛出异常,然后又catch回去了,再接着打印了两次"hi finally"....

 

这个bug肯定会在2.8中修复。

 

0
0
分享到:
评论
1 楼 蔡华江 2010-08-20  
初学scala。
楼主的代码在2.8中表现同样神奇。
引用
Exception in thread "main" java.lang.RuntimeException: ouch
at org.ocasoframework.HelloWorld$.main(HelloWorld.scala:13)
at org.ocasoframework.HelloWorld.main(HelloWorld.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
hi
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
in finally

感觉throw出的exception与两个printf不是处于同一个线程了

相关推荐

Global site tag (gtag.js) - Google Analytics