捕获在Scala中抛出的异常 - 不可达的catch块 [英] Catch in Java a exception thrown in Scala - unreachable catch block
本文介绍了捕获在Scala中抛出的异常 - 不可达的catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Scala 没有检查例外。但是,从java调用scala代码时,最好捕获scala抛出的异常。
Scala doesn't have checked exceptions. However, when calling scala code from java, it's desirable to catch exceptions thrown by scala.
Scala:
def f()=
{
//do something that throws SomeException
}
Java:
try
{ f() }
catch (SomeException e)
{}
javac不喜欢,并且抱怨这个异常从未从try语句体中抛出
javac doesn't like this, and complains that "this exception is never thrown from the try statement body"
有没有办法让scala声明它抛出一个被检查的异常?
Is there a way to make scala declare that it throws a checked exception?
推荐答案
使用 throws
注释:
@throws(classOf[SomeException])
def f()= {
//do something that throws SomeException
}
您也可以注释类构造函数:
class MyClass @throws(classOf[SomeException]) (arg1: Int) {
}
这篇关于捕获在Scala中抛出的异常 - 不可达的catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文