Exceptions/Errors/Warnings in an S-like language

Stimulated in part by Luke Tierney's notes, here are a few ramblings on the subject. These are not intended to take any strong positions. From some moderate experience with Java, I do think exceptions as objects are helpful and, on the whole, Java's approach has been useful to me.

How to best adapt this general idea to languages of the S or R flavor needs thinking.

Also, I would like to feel that any new features introduced could be done in a way that would flow into the Omega world as well.

What would S or R gain from the exception mechanism?

  1. differentiation among different classes of errors;
  2. more flexibility in dealing with errors (since a version of the try mechanism lets us control errors for arbitrary expression evaluation rather than just at the function level);
  3. the ability to carry information with the error object;
  4. potentially, the integration of (and extension of) warnings as well.

The Java mechanism:


  try{expr} catch(decl1) {e1} ... catch(declk) {ek} finally{ef}
strikes me as very like a method-dispatching specification. Each of the decli type declarations specifies a class of exceptions; when an exception is thrown, the evaluator tries to match the declared classes to the actual exception and evaluates the corresponding ei, with the ``argument'', the variable in the declaration, set to the actual exception.

Without changing the S/R syntax, this rather strongly suggests something of the form:


  try(expr, type1 = f1, ... , typek = fk, ef)
where now typei is the name of an exception class, fi is a function object taking (presumably) one argument which will be the exception object, and ef if present is the finally expression.

I believe this mechanism would include warnings as well; offhand, the distinction seems to a first approximation to be just what the default ``handler'' is for the class: the classes error and warning would have default handlers approximating the current behavior.

Of course, this includes the special case of pre-defined error and warning classes.

How the distinction between ``exit'' and ``call'' exceptions enters into this is unclear to me. There is a potential distinction as to whether the handler function fi is called from the context where the exception is thrown or from the context where the try call occurred.

Also, Java has the notion of rethrowing the caught exception; as Luke surmised, there is the option to rethrow it from where the catch occurs or from the original context (by calling the fillInStackTrace method (e.g. Java Language Reference, p. 232). This certainly has an obvious parallel in the notion of the corresponding evaluation frames in S.

A mechanism of this sort would (barring some pretty awful kludges) require some change to the S evaluator. But nothing that in any way seems to violate the basic philosophy as far as I can see at this point.

John Chambers<jmc@research.bell-labs.com>
Last modified: Tue Jun 8 23:13:33 Local time zone must be set--see zic manual page 1999