tmporaries
tmporaries

Reputation: 1553

Scala: Silently catch all exceptions

Empty catch block seems to be invalid in Scala

try {
  func()
} catch {

} // error: illegal start of simple expression

How I can catch all exceptions without their processing?

Upvotes: 17

Views: 36272

Answers (8)

som-snytt
som-snytt

Reputation: 39577

If annotating with Throwable is burdensome, you can also

scala> def quietly[A]: PartialFunction[Throwable, A] = { case _ => null.asInstanceOf[A] }
quietly: [A]=> PartialFunction[Throwable,A]

scala> val x: String = try { ??? } catch quietly
x: String = null

which has the small virtue of being self-documenting.

scala> val y = try { throw new NullPointerException ; () } catch quietly
y: Unit = ()

Edit: syntax changes include fewer braces for try-catch, catching with a function and function literals taken as partial functions:

scala> def f(t: Throwable) = ()
def f(t: Throwable): Unit

scala> try throw null catch f
                            ^
       warning: This catches all Throwables. If this is really intended, use `case _ : Throwable` to clear this warning.

scala> def f: PartialFunction[Throwable, Unit] = _ => ()
def f: PartialFunction[Throwable,Unit]

scala> try ??? catch f

Upvotes: 3

leo9r
leo9r

Reputation: 2047

What about:

import scala.util.control.Exception.catching
catching(classOf[Any]) opt {func()}

It generates an Option like in some of the previous answers.

Upvotes: 0

Johnny
Johnny

Reputation: 15413

I like one of the suggestions here to use Try() with toOption, but in case you don't want to go further with Option, you can just do:

Try(func()) match {
  case Success(answer) => continue(answer)
  case Failure(e) => //do nothing
}

Upvotes: 2

Rex Kerr
Rex Kerr

Reputation: 167871

Some exceptions really aren't meant to be caught. You can request it anyway:

try { f(x) }
catch { case _: Throwable => }

but that's maybe not so safe.

All the safe exceptions are matched by scala.util.control.NonFatal, so you can:

import scala.util.control.NonFatal
try { f(x) }
catch { case NonFatal(t) => }

for a slightly less risky but still very useful catch.

Or scala.util.Try can do it for you:

Try { f(x) }

and you can pattern match on the result if you want to know what happened. (Try doesn't work so well when you want a finally block, however.)

Upvotes: 38

om-nom-nom
om-nom-nom

Reputation: 62835

Just for a sake of completeness, there is a set of built-in methods in Exception, including silently catching exceptions. See Using scala.util.control.Exception for details on usage.

Upvotes: 2

elm
elm

Reputation: 20405

In

import scala.util.Try

val res = Try (func()) toOption 

if the Try is successful you will get a Some(value), if it fails a None.

Upvotes: 5

Akos Krivachy
Akos Krivachy

Reputation: 4966

Inside of Scala's catch block you need to use similar construct as in a match statement:

try {
  func()
} catch {
  case _: Throwable => // Catching all exceptions and not doing anything with them
}

Upvotes: 3

Tamere
Tamere

Reputation: 101

Try adding

case x =>

in your catch block :)

Upvotes: 3

Related Questions