Catch all exceptions in Scala 2.8 RC1

Posted by Michel Krämer on Stack Overflow See other posts from Stack Overflow or by Michel Krämer
Published on 2010-04-28T05:48:31Z Indexed on 2010/04/28 5:53 UTC
Read the original article Hit count: 357

I have the following dummy Scala code in the file test.scala:

class Transaction {
  def begin() {}
  def commit() {}
  def rollback() {}
}

object Test extends Application {
  def doSomething() {}

  val t = new Transaction()
  t.begin()
  try {
    doSomething()
    t.commit()
  } catch {
    case _ => t.rollback()
  }
}

If I compile this on Scala 2.8 RC1 with scalac -Xstrict-warnings test.scala I'll get the following warning:

test.scala:16: warning: catch clause swallows everything: not advised.
    case _ => t.rollback()
    ^
one warning found

So, if catch-all expressions are not advised, how am I supposed to implement such a pattern instead? And apart from that why are such expressions not advised anyhow?

© Stack Overflow or respective owner

Related posts about scala

Related posts about catch-all