Useful alternative control structures?

后端 未结 28 923
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

28条回答
  •  生来不讨喜
    2021-01-30 02:49

    ignoring - To ignore exceptions occuring in a certain block of code.

    try {
      foo()
    } catch {
      case ex: SomeException => /* ignore */
      case ex: SomeOtherException => /* ignore */
    }
    

    With an ignoring control construct, you could write it more concisely and more readably as:

    ignoring(classOf[SomeException], classOf[SomeOtherException]) {
      foo()
    }
    

    [ Scala provides this (and many other Exception handling control constructs) in its standard library, in util.control package. ]

提交回复
热议问题