Apache有IOUtils.closeQuietly(Closeable)
。在Scala中,我想概括一下:执行一个块并插入异常,同时记录它们。这样的事情:,执行一个块并忽略但记录异常
import LogUtils._
object Playground extends App {
implicit val logger_ = LoggerFactory.getLogger(getClass)
silentLog {
println("block")
throw new Exception("an exception")
}
println("end")
}
import org.slf4j.{Logger, LoggerFactory}
import scala.util.control.NonFatal
object LogUtils {
def silentLog[U](f: => U)(implicit log: Logger) {
try f
catch {
case NonFatal(e) => log.error(null, e)
}
}
}
这是否已经在一些常用的库中实现?
我想不会,因为通常不是一个好的做法 – cchantep