使用 scala.util.control.Exception

发布于 2024-09-03 05:12:44 字数 318 浏览 3 评论 0原文

有没有人有使用 scala.util.control.Exception 版本 2.12.0 (版本 2.8.0), ?我正在努力从类型中找出答案。

Does anybody have good examples of using scala.util.control.Exception version 2.12.0 (version 2.8.0), ? I am struggling to figure it out from the types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2024-09-10 05:12:44

确实 - 我也觉得这很令人困惑!这是一个问题,我有一些可能是可解析日期的属性:

def parse(s: String) : Date = new SimpleDateFormat("yyyy-MM-dd").parse(s)
def parseDate = parse(System.getProperty("foo.bar"))

type PE = ParseException
import scala.util.control.Exception._

val d1 = try { 
             parseDate
           } catch { 
             case e: PE => new Date
           }

将其切换为函数形式:

val date =
     catching(classOf[PE]) either parseDate fold (_ => new Date, identity(_) ) 

在上面的代码中,将捕捉(t)任一expr将导致Either[ T, E] 其中 T 是 throwable 的类型,E 是表达式的类型。然后可以通过折叠将其转换为特定值。

或者另一个版本:

val date =
     handling(classOf[PE]) by (_ => new Date) apply parseDate

这可能更清楚一些 - 以下是等效的:

handling(t) by g apply f 
try { f } catch { case _ : t => g }

Indeed - I also find it pretty confusing! Here's a problem where I have some property which may be a parseable date:

def parse(s: String) : Date = new SimpleDateFormat("yyyy-MM-dd").parse(s)
def parseDate = parse(System.getProperty("foo.bar"))

type PE = ParseException
import scala.util.control.Exception._

val d1 = try { 
             parseDate
           } catch { 
             case e: PE => new Date
           }

Switching this to a functional form:

val date =
     catching(classOf[PE]) either parseDate fold (_ => new Date, identity(_) ) 

In the above code, turns catching(t) either expr will result in an Either[T, E] where T is the throwable's type and E is the expression's type. This can then be converted to a specific value via a fold.

Or another version again:

val date =
     handling(classOf[PE]) by (_ => new Date) apply parseDate

This is perhaps a little clearer - the following are equivalent:

handling(t) by g apply f 
try { f } catch { case _ : t => g }
山有枢 2024-09-10 05:12:44

以下是一些示例:

compiler/scala/tools/nsc/interpreter/InteractiveReader.scalacompiler

  def readLine(prompt: String): String = {
    def handler: Catcher[String] = {
      case e: IOException if restartSystemCall(e) => readLine(prompt)
    }
    catching(handler) { readOneLine(prompt) }
  }

/scala/tools/nsc/Interpreter.scalacompiler

  /** We turn off the binding to accomodate ticket #2817 */
  def onErr: Catcher[(String, Boolean)] = {
    case t: Throwable if bindLastException =>
      withoutBindingLastException {
        quietBind("lastException", "java.lang.Throwable", t)
        (stringFromWriter(t.printStackTrace(_)), false)
      }
  }

  catching(onErr) {
    unwrapping(wrapperExceptions: _*) {
      (resultValMethod.invoke(loadedResultObject).toString, true)
    }
  }

...

  /** Temporarily be quiet */
  def beQuietDuring[T](operation: => T): T = {
    val wasPrinting = printResults
    ultimately(printResults = wasPrinting) {
      printResults = false
      operation
    }
  }

  /** whether to bind the lastException variable */
  private var bindLastException = true

  /** Temporarily stop binding lastException */
  def withoutBindingLastException[T](operation: => T): T = {
    val wasBinding = bindLastException
    ultimately(bindLastException = wasBinding) {
      bindLastException = false
      operation
    }
  }

/scala/tools/nsc/io/Process.scalalibrary

  def exitValue(): Option[Int] =
    catching(classOf[IllegalThreadStateException]) opt process.exitValue()

/scala/xml/include/ sax/Main.scala

def saxe[T](body: => T) = catching[T](classOf[SAXException]) opt body

...

   ignoring(classOf[SAXException]) {
     includer.setProperty(lexicalHandler, s)
     s setFilter includer
   }

Here are some examples:

compiler/scala/tools/nsc/interpreter/InteractiveReader.scala

  def readLine(prompt: String): String = {
    def handler: Catcher[String] = {
      case e: IOException if restartSystemCall(e) => readLine(prompt)
    }
    catching(handler) { readOneLine(prompt) }
  }

compiler/scala/tools/nsc/Interpreter.scala

  /** We turn off the binding to accomodate ticket #2817 */
  def onErr: Catcher[(String, Boolean)] = {
    case t: Throwable if bindLastException =>
      withoutBindingLastException {
        quietBind("lastException", "java.lang.Throwable", t)
        (stringFromWriter(t.printStackTrace(_)), false)
      }
  }

  catching(onErr) {
    unwrapping(wrapperExceptions: _*) {
      (resultValMethod.invoke(loadedResultObject).toString, true)
    }
  }

...

  /** Temporarily be quiet */
  def beQuietDuring[T](operation: => T): T = {
    val wasPrinting = printResults
    ultimately(printResults = wasPrinting) {
      printResults = false
      operation
    }
  }

  /** whether to bind the lastException variable */
  private var bindLastException = true

  /** Temporarily stop binding lastException */
  def withoutBindingLastException[T](operation: => T): T = {
    val wasBinding = bindLastException
    ultimately(bindLastException = wasBinding) {
      bindLastException = false
      operation
    }
  }

compiler/scala/tools/nsc/io/Process.scala

  def exitValue(): Option[Int] =
    catching(classOf[IllegalThreadStateException]) opt process.exitValue()

library/scala/xml/include/sax/Main.scala

def saxe[T](body: => T) = catching[T](classOf[SAXException]) opt body

...

   ignoring(classOf[SAXException]) {
     includer.setProperty(lexicalHandler, s)
     s setFilter includer
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文