Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!whuxl!whuxlm!akgua!gatech!seismo!utah-cs!brownc From: brownc@utah-cs.UUCP Newsgroups: net.lang.mod2 Subject: Exceptions in Modula-2 Message-ID: <3661@utah-cs.UUCP> Date: Sat, 1-Feb-86 01:39:12 EST Article-I.D.: utah-cs.3661 Posted: Sat Feb 1 01:39:12 1986 Date-Received: Sun, 2-Feb-86 01:05:00 EST Reply-To: brownc@utah-cs.UUCP (Eric C. Brown) Organization: University of Utah, Salt Lake City Lines: 71 This is a message from a friend of mine; I *DID NOT* write this. However, I *will* forward responses to him. ----------------------------------------------------------------------------- There have been several proposals to extend Modula 2 to include exceptions. I believe that extending the language is not needed. Below is a small definition module that I think will take the place of the exceptions proposed. The implementation module should not be difficult to write. Most of it can be stolen from the setjmp and longjmp code for C. Of course the implemenation module will be machine specific. This code was inspired by Lisp's catch and throw. For those who are not familar with lisp catch takes a tag (any atom) and an expression to evaluate. If in the process of evaluating the expression a throw is executed, the system goes hunting for the catch with the same tag. There is no easy way to implement a tags in modula because each module may want to add more tags. There are several possible solutions: use cardinals or integers and have the user assure that the numbers are different between each different case, or use strings. Strings are attractive since they are much more descriptive that just a number. Notice in the definition module below there is no dependence on the size of the string. The implementation module may only use a limited size string (e.g. only the first 32 chars are significant). DEFINITION MODULE Exceptions; EXPORT QUALIFIED CatchResult, Catch, Throw, Caught; TYPE CatchResult = (NormalExit, Thrown); PROCEDURE Catch(tag: ARRAY OF CHAR; p: PROC): CatchResult; PROCEDURE Throw(tag, prompt: ARRAY OF CHAR); PROCEDURE Caught(tag: ARRAY OF CHAR): BOOLEAN; END Exceptions. One other nice feature is the function Caught. This allows the lower level module to take care of the problem itself. Example: IF Caught("DiskFull") THEN Throw("DiskFull"); ELSE WriteString("***** FATAL ERROR ***** The disk is full!"); HALT; END; (* IF *) I have not yet implemented the implementation module because I have not needed exceptions. I would like to some other discussion of proposed extentions to Modula 2. In February's Computer Language there is a discussion of many extensions to the language. In my opinion most of them are not needed. Any other comments? Jeffrey McArthur ------------------------------------------------------------------------------ End of forwarded message. Eric C. Brown UUCP: ...!{decvax, ihnp4, seismo}!utah-cs!brownc ARPA: brownc@utah-cs.ARPA