Skip to content
richhickey edited this page Jun 18, 2011 · 6 revisions

Exception handling is fairly similar in structure in Java and JavaScript, with one notable exception: a Java catch block matches on a type, and a JavaScript one does not. How should we resolve this in the Clojure API?

Options

  1. catch in ClojureScript simply has one less argument than catch in Java (catch is about platform interop, and platform interop forms can and will differ)
  • RH - try/catch is not only about interop - it is the only error handling mechanism in Clojure
  1. catch in ClojureScript has a dummy arg that is ignored
  2. catch in ClojureScript tries to simulate the semantics provided in Javaland
  3. ClojureScript uses a different name than 'catch' so that people aren't caught off guard by the different API and semantics

I think #1 wins, and have already implemented it. Reasons:

  • Platform interop will be different and look different where the semantics are different. We have already seen this with the dot (.) form.

  • catch is clearly an interop form, not a manifestation of the Clojure Way to deal out-of-band with errors. The latter would be dynamic binding of a handler, which has two big advantages over exceptions: ** action at the point of failure ** composability with all our other abstractions to enable flexibility (in particular protocols and multimethods) *** RH - No handler system stands alone independent of a non-local transfer of control mechanism (i.e. what do you do if no handler is registered, or the handler would like you to abort?). And the "Clojure Way" you describe is an idea with few extant implementations, certainly none in core.

  • If we try to fake the Java syntax or semantics, we are just encouraging people to do the wrong thing (i.e. conditionalizing error handling with types). It would suck to see people adding this to their JavaScript world because we encouraged it. ** RH - I'm not sure we are encouraging anything, any more than having atoms is encouraging people to use them as local variables ** RH - different 'types' of exceptions are already thrown by JS. To the extent people are already building conditionals around those types, this provides a simpler mechanism. People who don't care can just catch Error.

Admittedly, approach #1 will cause pain for people who are porting Clojure libs to ClojureScript. Every place that catches a Java exception will be broken, and have to be handled differently in ClojureScript. These break down into two cases:

  1. Places where the Java exception handling cares about the type of the exception. In this scenario you are screwed anyway, no matter what we do. The JavaScript side will already be different, and papering it over in the catch syntax won't prevent the need for separate code.
  • RH - Eventually there will have to be another answer besides 'you're screwed' in this area where the types simply must differ, as well as other areas; i.e. conditional compilation or such. If you encapsulate the types you are going to have to unify access to the error information they contain. That would be hard enough to do on the Java side, where there are already some standards. People can throw random things in JS.
  1. Places where the Java exception handling does not (or should not) care about the exception type. If there are a ton of these, and people's code could be the same across Clojure and ClojureScript except for the catch block syntax, I would recommend offering a new "catchall" form. Where catch is for platform interop, catchall is the Clojure way. catchall would catch all exceptions that application code should ever catch, and would provide no way to match on type. It would map to (catch) in ClojureScript, and to (catch Exception) in Clojure.
  • RH - And after you've caught-all an 'opaque blob' what do you do with it?
  1. RH - If Clojure had a standard exception 'type', it could be present in both. Throwing and catching that could be portable. Getting information out of it would be portable. I don't see how you get out of being able to distinguish what was thrown though, if you can't force everything to use it.
  • The 'problem' of exception type proliferation is not due to having a catch that can distinguish, but to the failure to provide an extensible, data-conveying exception type in the first place, and the convention of using types to convey data. Given an extensible data-conveying type you can avoid that problem by simply using it. However, it doesn't save you from having to deal with errors generated by people with a different approach.

It is worth noting that while the goog library's static typing appears to support verifying exception types at the point of a throw, but they have made no effort I can see to branch on type at the point of catch. If they don't think this is good/doable, we probably shouldn't either.

  • RH - Conventions work in a single shop.
  • RH - Are we making up a new language and VM here? If so, this kind of thinking would be on the table, and I might agree. But we are supposed to be subsetting, and this seems like a rather unimportant place to introduce an incompatibility.

I didn't give much consideration to option 4, using a different name than "catch". The name has too much good connotation, and we have already set the pattern (with the dot operator) of using the existing names and letting them be slightly different.

  • RH - I don't understand this sentence. dot is an interop form, designed to capture as much of what dot means as makes sense for the platform. The platforms are slightly different.

RH - I'm unconvinced by your argument, in particular the lack of identification of any deep problems with option #3 and portability approach #3

Clone this wiki locally