Following up my previous question I wonder if resumable exceptions make any sense. It looks like nobody uses them in Java. Don't they use resumable exceptions because of Java limitations or just because they don't really need them?

Suppose, I want to create a file in a directory, which should exist but does not. I would probably prefer use a resumable exception (i.e. throw an exception, catch it and create the directory, and resume the execution) instead of testing if the directory exists every time I create a file. Does it make sense in general ?

  • 2
    (Java has resumable exceptions? I'd imagine they are unused due to lack of ... support.) In any case, I am not fan of the idea in general -- but this is likely because I was not weened on SmallTalk or CLISP and only have bad memories of on error RESUME next. – pst May 28 '11 at 6:54
  • Java does not have resumable exceptions and this question is a bit too argumentative for SO, I think. – Kim May 28 '11 at 7:23
  • It would probably be hard to implement resumable exceptions in Java since it doesn't have resumable continuations. In any case it wouldn't be too elegant to use IMO. – haknick May 28 '11 at 12:34
  • 1
    Actually, resumable exceptions can be implemented without using continuations. You really only need a global exceptionHandler variable, which is set to a new handler in the extend of a try/catch block, and raising the exception is simply calling exceptionHandler. However in Java this would be way too verbose. – hzap May 28 '11 at 17:52
  • 1
    Resumable exceptions make it trivial to implement delimited dynamic variables. – Frank Shearar Jun 26 '12 at 15:05

Not directly related to Scala, but when the C++ exception mechanism was being designed, there were big arguments about resumption. Eventually, they looked at a very large codebase (not of C++) where resumption could have been used, and found only one instance of it, which could easily be re-written not to use it. Also, C++ committee members who had used resumption were solidly of the opinion that it caused nothing but problems. So it was decided that C++ should not support exception resumption. Designers of most other languages seem to be of the same opinion.

| improve this answer | |
  • 2
    +1 hear hear. If you need to resume some operation after an exception, just structure your code in such a way that you can easily do it explicitly, whether by re-invoking the failed function or otherwise. Hell, use a goto if it excites you. – Jon Purdy May 30 '11 at 18:41

The real point of resumable exceptions is that the place where the exception happens is often not the right place to decide what to do about it. The OP's missing directory exception may occur inside some low level utility library, which which can be used by many different systems.

If the client code is a batch program then there may be nothing better to do than clean up, log the error and die. However, if the error occurs in an interactive application it makes perfect sense to prompt the user to either create the directory or specify an alternative one and continue. You do not want the utility library to know about the different ways to deal with the missing directory and throwing an ordinary exception loses your current context/state.

With resumable exceptions (or a continuation) you can let client code decide what to do about the missing directory with the option of resuming from where the error occurred, possibly using a different directory.

The utility library just throws a resumable exception (or an exception containing the current continuation) without any coupling to how the issue may possibly be resolved.

| improve this answer | |

I believe resumable exceptions are more useful in language platforms where it is feasible to catch an exception, use the debugger to isolate the problem, change the code to fix the problem, and update the running system with the new code before resuming the program. Think the Lisp Machine, and maybe Smalltalk. Modern systems have moved away from that level of interactivity; I've never used a system like that (except emacs, but I'm a shallow emacs user), so take my answer with a grain of salt.

Note that resumable continuations are similar to continuations, which have other uses in their own right.

| improve this answer | |
  • 1
    Smalltalk definitely supports resumable exceptions. Common Lisp, too. Certainly in Smalltalk it's not a particularly rare technique. – Frank Shearar May 31 '11 at 10:04
  • Users (as opposed to smalltalk programmers) who find themselves being dumped into the debugger may wish it were rarer. – Neil Butterworth Jun 5 '11 at 15:50
  • 2
    @nbt Resumable exceptions have nothing to do with bugs that drop users into debuggers. – Frank Shearar Aug 21 '12 at 20:07

A Smalltalk web framework 'Seaside' uses resumable expections.

The gain: You can write code which follows a users walk through the web-pages (asks and resumes).

The alternative (with common web-frameworks and languages which do not support resumable exceptions) is to split your code into small, unrelated chunks and session-state.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.