Matching (and binding) two exception classes in one case statement in Scala 2.7?

Viewed 1423

I have the following code:

try {
    < ... some JSON parsing code .. >
} 
catch {
    case e:ClassCastException => throw new ParseException(body, e)
    case e:JSONException => throw new ParseException(body, e)
}

This seems overly repetitious. I tried

case e:ClassCastException | e:JSONException => throw new ParseException(body, e)

but Scala won't let me bind e to both types - fair enough. In the handler, I only need to treat e as if it were of type Exception, but I only want to match in the first place if it's one of those specific classes. Something like having a condition after the matched type, like:

case e:Exception(ClassCastException|JSONException) => throw new ParseException(body, e)

That's obviously not the right syntax, but hopefully you see what I mean. Is such a thing possible?

2 Answers
Related