Scala 2.9 的 try...catch 泛化有哪些用例? [英] What are the use cases for Scala 2.9's try...catch generalization?

查看:34
本文介绍了Scala 2.9 的 try...catch 泛化有哪些用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读并尝试了 Scala 2.9 try...catch 功能,它让我思考了各种可能性.除了节省几行代码之外,我实际上还能用它做什么?

I've read about and experimented with the Scala 2.9 try...catch feature, and it has me thinking about possibilities. What would I actually use it for other than saving a couple of lines of code?

Scala 2.9 最终发行说明

推荐答案

用例是能够在整个应用程序中进行通用错误处理.假设您想通过向管理员发送电子邮件来处理应用程序中的所有 FileNotFoundException.以前,您必须这样做:

The use case is to be able to have generic error handling throughout your application. Let's say you want to handle all FileNotFoundExceptions in your application by sending an e-mail to an administrator. Previously, you'd have to do it like this:

// Globally
val fileNotFound: PartialFunction[Throwable, Unit] = {
  case e: FileNotFoundException =>
    // Create report and send the e-mail
}

// On each try-catch-block
try {
  // Open file
} 
catch {
  case fnf: FileNotFoundException => fileNotFound(fnf)
}

现在你只需要做:

try {
  // Open file
} catch fileNotFound

这也有一个很好的优点,你可以在部分函数上使用 orElse 方法链接几个这样的异常处理程序:

This also has the nice advantage that you can link several such exception handlers using the orElse method on partial functions:

val fileErrors = fileNotFound orElse endOfFile orElse invalidFormat

然后在需要文件异常处理的任何地方使用它.例如,可以基于应用程序的配置文件动态组合这样的错误处理程序.这比无处不在的模式匹配和调用正确的处理程序要简单得多.

And then just use that everywhere where you need file exception handling. Such an error handler can be dynamically combined based on the configuration file for the application, for example. This is much less cumbersome than pattern matching everywhere and calling the correct handler.

一个有用的东西可以在偏函数之上拉皮条是 andAlso 运算符,它充当两个偏函数的排序运算符.如果您想在完成通用错误处理后针对特定的 try-catch 块进行一些特定的错误处理,这将非常有用.

One useful thing which could be pimped on top of partial functions is the andAlso operator, which acts as a sequencing operator on two partial functions. This would be useful when you want to do some error handling specific to a particular try-catch block after having done the generic error handling.

implicit def pf2ops(pf: PartialFunction[Throwable, Unit]) = new {
  def andAlso(localpf: PartialFunction[Throwable, Unit]) = new PartialFunction[Throwable, Unit] {
    def apply(t: Throwable) = {
      if (pf.isDefinedAt(t)) pf(t)
      localpf(t)
    }
    def isDefinedAt(t: Throwable) = pf.isDefinedAt(t) || localpf.isDefinedAt(t)
  }
}

然后你可以这样做:

scala> try {
     |   throw new java.io.FileNotFoundException
     | } catch fnf andAlso {
     |   case e: Exception => println("I don't know, but something is specific to this particular block.")
     | }
I don't know, but something is specific to this particular block.

我想您可以进一步了解 andAlso 的确切语义和含义(和名称).

I guess you could play further with the exact semantics and the meaning (and the name) of andAlso.

这篇关于Scala 2.9 的 try...catch 泛化有哪些用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆