15 Exceptions

The pattern exception defined in '~beta/basiclib/betaenv' is used as a superpattern for all exceptions in the Mjølner System. The default action of an exception is to stop the program execution and print an informative error message on the screen. In addition, the file <programname>.dump contains a dump of the call stack. Exception uses the pattern Stop for termination. Specific error messages can be defined by specializing the exception pattern. The attribute msg of exception is a text object that is used to accumulate error messages . If you wish to prevent the program execution from being stopped in order to handle the exception during execution, the boolean attribute continue of exception must be set to true.

The exceptions are often defined as a virtual pattern of other patterns (such as the file pattern, discussed below).

In order to differentiate between potential fatal exceptions and more harmless exceptions, the notification pattern is also defined in betaenv defined as:

notification: exception(# do true->continue; INNER #);

15.1 14.1 Examples Using Exception

In order to illustrate the use of exceptions, let us return to the previous file example. Without using the exception handling facilities an attempt to open a non-existing file produced the following error messages:

nil% CountChar

**** Exception processing
File exception for 'data1'
No such file

# Beta execution aborted: Stop is called
# Look at CountChar.dump'

Now let us see what can be done by using exceptions.

The binding of noSuchFileError shows how to prevent the system from stopping the execution when the program attempts to open a non-existing file. Instead the user is prompted for another file name. The binding of noSpaceError shows that a message can be added to msg.

ORIGIN '~beta/basiclib/file'
(# inFile: @file 
     (# noSuchFileError:: (* continue execution *)
          (# do true->continue; false->OK #)#);
   outFile: @file
     (# noSpaceError:: (* extend exception; put message to msg  *)
         (# do 'It is time to delete garbage!'->msg.putline #)#);
   OK: @boolean; 
   
do 'in.bet' -> inFile.name;
   true -> OK;
   openFile: (* labeled block *)
   (# 
   do inFile.openRead;
      (if not OK then
         'File does not exist!' ->  screen.putline;
         'Type input file name: ' ->  screen.puttext;
         inFile.readFileName;
         true -> OK;
         restart openFile (* restart labeled block *)
   if)#);
   
   'out.bet' -> outFile.name;
   outFile.openWrite;
   readFile: 
   (# 
   do (if not inFile.eos then 
      false -> inFile.gettext -> outFile.puttext;
      outFile.newline;
      restart readFile
    else leave readFile
   if)#);
   inFile.close;
   outFile.close;
#)

An attempt to open a non-existing file will produce the following error messages:

File does not exist!
Type input file name:

It gives the possibility to proceed with another file name.

In case of disk space exhausted, the following message will be printed on the screen before the program execution is stopped:

**** Exception processing
Error in file 'in.bet'
File system is full
It is time to delete garbage!

The first line is from the general pattern exception, the second and the third lines are from the binding of noSpaceError in file and the fourth line is from the binding above, i.e. at the user level.


Libraries Tutorial
© 1994-2004 Mjølner Informatics
[Modified: Friday April 6th 2001 at 12:43]