EXCEPTION

[Other proposals]

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).

After a marker is executed or a word is forgotten that was defined before EXCEPTION was called, THROWing n is an ambiguous condition.

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

The ambiguous condition after forgetting allows systems to reclaim exception numbers and the memory taken up by the strings on forgetting. Systems are not required to do this.

Andrew Haley also voiced concerns about the interaction with multitasking systems. This proposal can be integrated with multitasking systems in several ways:

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.

EXCEPTION is used in the garbage collector.

Comments

Michael L. Gassanenko on experience:
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:
another part of your "library" model, most useful.

Anton Ertl