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. The thing is, 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 you see what I mean hopefully. Is such a thing possible?