如何使用拦截将异常捕获代码从java转换为scala?
从阅读http://www.scalatest.org/getting_started_with_fun_suite开始,似乎建议在捕获异常时使用拦截。在下面的异常捕获代码中,我需要访问Exception类型,以便在其中检查其类型 断言:
catch {
case me : MyException => {
assert(me.getDetail.getClass() === classOf[GenericException]);
}
}
这似乎不可能使用拦截,因为下面的代码会导致编译器错误:'前向引用扩展到值异常的定义'
这是转换为scala catch块:
val exception = intercept[MyException] {
assert(exception.getDetail.getClass() === classOf[GenericException]);
}
错误发生在
行assert(exception.getDetail.getClass() === classOf[GenericException]);
答案 0 :(得分:1)
你对exception
的定义是循环的。你想要的是:
val exception = intercept[MyException] {
<your exception-throwing code here>
}
assert(exception.getDetail.getClass() === classOf[GenericException])