From: eachus@mitre-bedford.ARPA (Robert Eachus)
Subject: Re: Ada exceptions
Date: 7 Sep 88 22:28:48 GMT [thread overview]
Message-ID: <39596@linus.UUCP> (raw)
In-Reply-To: 5701@utah-cs.UUCP
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...
next prev parent reply other threads:[~1988-09-07 22:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1988-09-06 22:07 Real time environment w/ 300K+ lines? MFHorn
1988-09-07 9:01 ` Ada exceptions Daniel M Floyd
1988-09-07 22:28 ` Robert Eachus [this message]
1988-09-09 16:57 ` Carl-Lykke Pedersen
1988-09-12 16:10 ` Robert Eachus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox