In the previous copy program, the FakeOS.Copy
procedure has a problem: it crashes when any I/O exception (e.g., disk
full, no permission to write) error is raised.
A more robust interface, FakeOS.i3
and its implementation FakeOS.m3
illustrate how to use exceptions. The main module
Exceptions.m3
calls the FakeOS
interface. The
required changes are:
Error
exception to the FakeOS
interface.
FakeOS.Copy
to raise Error
when there
is a problem, and include a text string describing the problem.
Main
module to handle this exception by printing a
message for the user.
You can try to test exception handling by copying a non-existent file.
This program is now robust against various system exceptions. If the program hadn't been handling a particular exception, you would have seen a warning at compile-time. This same program, without any source modifications will work without crashing due to system exceptions across any operating system supported by cm3.