尝试使用异常日志记录 [英] Try with exception logging
本文介绍了尝试使用异常日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Scala的Try
非常有用.
Scala's Try
is very useful.
我想使用该模式,但记录所有异常.
I'd like to use that pattern, but log all exceptions.
我该怎么做?
推荐答案
定义以下帮助程序:
import scala.util.{Try, Failure}
def LogTry[A](computation: => A): Try[A] = {
Try(computation) recoverWith {
case e: Throwable =>
log(e)
Failure(e)
}
}
然后您可以像使用Try
一样使用它,但是任何异常都将通过log(e)
记录.
Then you can use it as you would use Try
, but any exception will be logged through log(e)
.
这篇关于尝试使用异常日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文