Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching on nested exception type

Tags:

scala

I am using some client library and had some code that ignored a specific exception using scala.util.control.Exception.ignoring:

ignoring(classOf[ReallyNotThatExceptionalException]) {
  stuff()
}

Now that library changed to wrap all exceptionn in another exception class, which forced me to change my code to this:

try { stuff() }
catch {
  case e:WrapperException if e.getCause != null && e.getCause.isInstanceOf[ReallyNotThatExceptionalException] => { }
}

So what I'm looking for a more readable way to express "catch exceptions that are caused by".

like image 344
c089 Avatar asked Dec 12 '14 14:12

c089


1 Answers

0__'s answer is good, but it would be better if you were not forced to write a specific object (CausedByFoo) for each potential exception. As it happens, there is not much to change to end up with a generic CausedBy helper object:

class Foo(message: String) extends Exception(message)
class Bar(cause: Throwable) extends Exception(cause)

object CausedBy {
  def unapply(e: Throwable): Option[Throwable] = Option(e.getCause)
}

def test(block: => Unit): String = 
  try {
    block
    "ok"
  } catch {
    case CausedBy(ex: Foo) => "not ok: " + ex.getMessage
  }

test(println("hello"))
test(println("hello".toInt)) // uncaught exception
test(throw new Bar(new Foo("Ooops, foo error!"))) // caught

As should be obvious, you can use CausedBy with any exception (by example by doing case CausedBy(ex: Baz).

You can even nest it to handle an exception caused by an exception caused by an exception (by doing something like case CausedBy(CausedBy(ex: Foo))

like image 148
Régis Jean-Gilles Avatar answered Sep 22 '22 00:09

Régis Jean-Gilles