Exceptions/Catch an exception thrown in a nested call: Difference between revisions

Content added Content deleted
No edit summary
Line 47:
</pre>
An unhandled exception leads to termination of the corresponding [[task]]. When the task is the main task of the program as in the example, the whole program is terminated. In the example the exception back tracing message is compiler-specific (in this case it is [[GNAT]] and further depends on the compiler options.
=={{header|ALGOL 68}}==
{{trans|python}}
 
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
The following example follows the method used by ALGOL 68 for handling events
in the language's ''transput'' preclude. Note that in the ''transput'', then exception
is effectively bound to the '''file''' handle, hence different '''file''' events can be caught
by event handler associated to that particular '''file'''. Similarly the following example
has bound the two exceptions ''u0'' & ''u1'' to each instance of '''object'''.
 
c.f. [[Exceptions#ALGOL_68|ALGOL 68 Exceptions]] for more details.
<lang algol>MODE OBJ = STRUCT(
INT value,
STRUCT(
STRING message,
FLEX[0]STRING args,
PROC(REF OBJ)BOOL u0, u1
) exception
);
 
PROC on u0 = (REF OBJ self, PROC (REF OBJ) BOOL mended)VOID:
u0 OF exception OF self := mended;
PROC on u1 = (REF OBJ self, PROC (REF OBJ) BOOL mended)VOID:
u1 OF exception OF self := mended;
 
PRIO INIT = 1, RAISE = 1;
 
OP INIT = (REF OBJ self, INT value)REF OBJ: (
value OF self := value;
u0 OF exception OF self := u1 OF exception OF self := (REF OBJ skip)BOOL: FALSE;
args OF exception OF self := message OF exception OF self := "OBJ Exception";
self
);
 
OP RAISE = (REF OBJ self, PROC (REF OBJ) BOOL mended)VOID:
IF NOT mended(self) THEN
put(stand error, (message OF exception OF self+" not caught - stop", new line));
stop
FI;
 
PROC (REF OBJ)VOID bar, baz; # early declaration is required by the ALGOL 68RS subset language #
 
PROC foo := VOID:(
FOR value FROM 0 TO 1 DO
REF OBJ i = LOC OBJ INIT value;
on u0(i, (REF OBJ skip)BOOL: (GO TO except u0; SKIP ));
bar(i);
GO TO end on u0;
except u0:
print(("Function foo caught exception u0", new line));
end on u0: SKIP
OD
);
 
# PROC # bar := (REF OBJ i)VOID:(
baz(i) # Nest those calls #
);
 
# PROC # baz := (REF OBJ i)VOID:
IF value OF i = 0 THEN
i RAISE u0 OF exception OF i
ELSE
i RAISE u1 OF exception OF i
FI;
 
foo</lang>
Note: when an event occurs there are three possible responses.
# return '''false''' - in which case the default action takes place.
# ''mend'' the '''object''' and return '''true''' - date is ''mended'' and the program can continue from the point the event was raised.
# jump to an appropriately named ''label'' - effectively abandoning the offending section of code.
 
In the case of parallel processing, if the ''label'' is outside of the
'''par''' clause, then all parallel the threads are terminated and the
program continues in the parent thread. <!-- example needed -->
Output:
<pre>
Function foo caught exception u0
OBJ Exception not caught - stop
</pre>
 
=={{header|C++}}==