From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!husc6!linus!mbunix!eachus From: eachus@mitre-bedford.ARPA (Robert Eachus) Newsgroups: comp.lang.ada Subject: Re: Ada exceptions Summary: You came to the right place Keywords: exceptions Message-ID: <39596@linus.UUCP> Date: 7 Sep 88 22:28:48 GMT References: <8957@swan.ulowell.edu> <5701@utah-cs.UUCP> Sender: news@linus.UUCP Reply-To: eachus@mitre-bedford.arpa (Robert I. Eachus) Organization: The MITRE Corporation, Bedford, Mass. List-Id: In article <5701@utah-cs.UUCP> u-dmfloy%sunset.utah.edu.UUCP@utah-cs.UUCP (Daniel M Floyd) writes: >This *must* be the right newsgroup. >In discussing the woes of other languages, I posted an >article about excpetion handling. I thought (and still think) >that some way should be available to specify what action >to take when an exception occurs. >For example: >I'd like to say to the compiler >/* exceptions sqrt(x) */ >/* if -x then do_this_process */ >/* if overflow then scale_routine_error */ >/* if underflow then return 0 */ >/* exceptions + */ >/* if overflow ... >I don't care what syntax is used. There are times when a halt >on a fatal error can be ... fatal. I can't tolerate it at times, so >I end up doing alot of firewalling before calling procedures, and >if I do alot of math, it can get ugly fast. What you want can be done easily in Ada, but it is not the normal Ada style. It is however very useful as a means of handling certain functions. For example, if I write a program which opens several files, I usually overload the OPEN call to provide reasonable handling of the "normal" error conditions. The more normal case is to have a handler cover a region of code (for example all the statements of a procedure) and do something sensible if an exception occurs in that region. Belt and suspenders types (and that includes most Ada software engineers) also put catastropic error handlers on the entire program. >I've had two messages that say "go Ada". >Can Ada really do it? If it can, I'll likely become a devotee, investing >within the month. Yes, but you still have to do the hard part... (Ada syntax and semantics are a big help, but you have to figure out what to do with the error. :-) >Could some Ada-ite post an Ada example like the one above, in Ada. First you need a square root routine. Most compilers come with math packages, but they are not strictly part of the language, and you can roll your own. Assume that the package you use says something like this: with MATH_EXCEPTIONS; generic type REAL is digits <>; -- some floating point type package MATHEMATICAL_FUNCTIONS is subtype NON_NEGATIVE is REAL range 0.0..REAL'LAST; ... function SQRT(L: in NON_NEGATIVE) return NON_NEGATIVE; -- Returns the positive square root of L. Result is accurate to -- within +/- 2.0*REAL'EPSILON times the correct result. Result is -- guarenteed to be greater than L for small positive L. SQRT(0.0) -- is always exactly 0.0. If 1.0 is a model number of type REAL, -- then SQRT(REAL(N*N)) = REAL(N) for any integer N if N*N is a -- model number of type REAL. ... end MATHEMATICAL_FUNCTIONS; -- Now for your program: with MATHEMATICAL_FUNCTIONS; with MATH_EXCEPTIONS; use MATH_EXCEPTIONS; procedure MAIN is X, Y, Z: FLOAT; package MATH is new MATHMATICAL_FUNCTIONS(FLOAT); procedure DO_THIS_PROCESS; ... function SQRT(X: in FLOAT) is begin if X < 0.0 then Do_This_Process; else return MATH.SQRT(X); end if; -- Your other cases don't arise... end SQRT; function "+" (L,R: FLOAT) is -- hides STANDARD."+" begin return STANDARD'"+"(L,R); exception when others => ... end "+"; ... begin ... Y := sqrt(X); -- Strong typing assures that no other error can -- occur. Even nicer than handling errors is to -- be able to guarentee that they can't happen. Z := X + Y; -- Your "+" which presumably doesn't overflow. end MAIN; You may think that the specification of SQRT is overkill, but it is "normal" Ada practice to specify which exceptions can be raised, and under what conditions, by any function in a library package. Robert I. Eachus with STANDARD_DISCLAIMER; use STANDARD_DISCLAIMER; function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...