In the previous section we saw an example of raising an exception when a call to New
returned NIL
.
We can re-write this example to use automatic exception raising:
CONST BIG_AMOUNT = 100000 ENUM ERR_MEM=1 RAISE ERR_MEM IF New()=NIL PROC main() HANDLE DEF block block:=New(BIG_AMOUNT) WriteF('Got enough memory\n') EXCEPT IF exception=ERR_MEM WriteF('Not enough memory\n') ELSE WriteF('Unknown exception\n') ENDIF ENDPROC
The only difference is the removal of the IF
which checked the value of block
, and the addition of a RAISE
part.
This RAISE
part means that whenever the New
function is called in the program, the exception ERR_MEM
will be raised if it returns NIL
(i.e., the exception ERR_MEM
is automatically raised).
This unclutters the program by removing a lot of error checking IF
statements.
The precise form of the RAISE
part is:
RAISE exception IF function() compare value , exception2 IF function2() compare2 value2 , ... exceptionN IF functionN() compareN valueN
The exception is a constant (or number) which represents the exception to be raised, function is the E built-in or system function to be automatically checked, value is the return value to be checked against, and compare is the method of checking (i.e., `=', `<>', `<', `<=', `>' or `>=').
This mechanism only exists for built-in or library functions because they would otherwise have no way of raising exceptions.
The procedures you define yourself can, of course, use Raise
to raise exceptions in a much more flexible way.
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.