Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 456
gfxmonk Avatar asked Jul 11 '10 10:07

gfxmonk


2 Answers

You can't introduce bindings inside of Pattern Alternatives (PatternA | PatternB). But you can bind a name to the result of Pattern Alternatives with a Pattern Binder (name @ Pattern).

try {
    < ... some JSON parsing code .. >
} catch {
    case e @ (_: ClassCastException | _: JSONException) => throw new ParseException(body, e)
}
like image 58
retronym Avatar answered Nov 13 '22 15:11

retronym


You could use the new 2.8 control constructs:

def foo = //JSON parsing code

import util.control.Exception._
handling(classOf[ClassCastException], classOf[JSONException]) by (t => throw new ParseException(t)) apply foo

(There's probably a mistake in there. I can't find an REPL for the jabscreen.)

like image 24
oxbow_lakes Avatar answered Nov 13 '22 16:11

oxbow_lakes