【发布时间】:2012-02-28 13:44:52
【问题描述】:
有没有办法访问在 finally 块中的 try/catch 块中创建的 val?或者是最终块超出范围。
def myTryCatch: Either[Exception, String] = {
try {
val w = runOrFailWithException("Please work...")
Right(w)
} catch {
case ex: Exception => {
Left(ex)
}
}
finally {
// How do I get access to Left or Right in my finally block.
// This does not work
_ match {
case Right(_) =>
case Left(_) =>
}
}
}
【问题讨论】:
-
你没有,最后只能看到声明在 try/catch 范围之外的东西,而不是在它里面。