Exceptions: Difference between revisions

Content added Content deleted
(→‎try-catch: langtag)
Line 512: Line 512:
===Defining exceptions===
===Defining exceptions===
Exceptions can only be declared at the "top-level" of a module or interface. Arguments are optional.
Exceptions can only be declared at the "top-level" of a module or interface. Arguments are optional.
<lang modula3>EXCEPTION EndOfFile;
<pre>
EXCEPTION EndOfFile;
EXCEPTION Error(TEXT);</lang>
EXCEPTION Error(TEXT);
</pre>


===Throw exceptions===
===Throw exceptions===
Exceptions can be bound to procedures using RAISES:
Exceptions can be bound to procedures using RAISES:
<lang modula3>PROCEDURE Foo() RAISES { EndOfFile } =
<pre>
PROCEDURE Foo() RAISES { EndOfFile } =
...
...
RAISE EndOfFile;
RAISE EndOfFile;
...
...
</pre>
</lang>


===Catching exceptions===
===Catching exceptions===
<lang modula3>TRY
<pre>
TRY
Foo();
Foo();
EXCEPT
EXCEPT
| EndOfFile => HandleFoo();
| EndOfFile => HandleFoo();
END;
END;</lang>
</pre>


Modula-3 also has a FINALLY keyword:
Modula-3 also has a FINALLY keyword:
<lang modula3>TRY
<pre>
TRY
Foo();
Foo();
FINALLY
FINALLY
CleanupFoo(); (* always executed *)
CleanupFoo(); (* always executed *)
END;
END;
</pre>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==