Exceptions: Difference between revisions

Content deleted Content added
→‎try-catch: langtag
Line 512:
===Defining exceptions===
Exceptions can only be declared at the "top-level" of a module or interface. Arguments are optional.
<lang modula3>EXCEPTION EndOfFile;
<pre>
EXCEPTION EndOfFileError(TEXT);</lang>
EXCEPTION Error(TEXT);
</pre>
 
===Throw exceptions===
Exceptions can be bound to procedures using RAISES:
<lang modula3>PROCEDURE Foo() RAISES { EndOfFile } =
<pre>
PROCEDURE Foo() RAISES { EndOfFile } =
...
RAISE EndOfFile;
...
</prelang>
 
===Catching exceptions===
<lang modula3>TRY
<pre>
TRY
Foo();
EXCEPT
| EndOfFile => HandleFoo();
END;</lang>
</pre>
 
Modula-3 also has a FINALLY keyword:
<lang modula3>TRY
<pre>
TRY
Foo();
FINALLY
CleanupFoo(); (* always executed *)
END;
</prelang>
 
=={{header|OCaml}}==