exception

Reference

MAE/1

Author

M. Anton Ertl (anton@mips.complang.tuwien.ac.at)

Problem

Libraries cannot introduce throw values, because they don't know which values are used by other libraries or the application.

The system does not know how to report a new exception.

Proposal

EXCEPTION ( c-addr u -- n ) exception
n is a previously unused THROW value in the range {-4095...-256}. Consecutive calls to EXCEPTION return consecutive decreasing numbers.

The system may use the string denoted by c-addr u when reporting that exception (if it is not caught).

Typical Use

s" Out of GC-managed memory" EXCEPTION CONSTANT gc-out-of-memory
...
... gc-out-of-memory THROW ...

Remarks

The restriction to values in the range {-4095...-256} ensures that existing standard programs continue to work.

The requirement to return consecutive decreasing THROW values makes it possible to check for whole classes of exceptions with WITHIN:

... CATCH ?DUP IF
  DUP lib-last-exception lib-first-exception 1+ WITHIN IF
    ... \ deal with exceptions from lib
  ELSE
    THROW \ just pass the ones on that we don't know how to handle
  THEN

Experience

EXCEPTION is implementend in Gforth since before release 0.4.0.

An approximation in ANS Forth is included in the Gforth compat library which is also included in the garbage collector.

Comments

Michael L. Gassanenko:
I used:
CREATE not-ready

... IF not-ready THROW ...

BTW, the system(s) that I have to work on have no built-in CATCH.

Peter Knaggs:
This is absolutely essential if REQUIRED is going to work. A library must be able to introduce it's own exceptions. The only problem I see here, is with regard to multilingual systems. The exception messages will be in the library author's language and not necessarily the language of the developer or user.