comp.lang.ada
 help / color / mirror / Atom feed
From: ka@socrates.hr.att.com (Kenneth Almquist)
Subject: Re: Why no exception hierarchy ?
Date: Sat, 25 Mar 1995 01:50:38 GMT
Date: 1995-03-25T01:50:38+00:00	[thread overview]
Message-ID: <D5z3sF.1ou@nntpa.cb.att.com> (raw)
In-Reply-To: gauthier-2403951115540001@164.81.60.62

An example of something that the Ada 83 exception mechanism did not handle
well is binding UNIX system calls to Ada.  POSIX.5 maps all system call
errors into a single exception named POSIX_Error, and provides a per-task
error code variable.  This is dangerous.  Consider the following C code:

	if (unlink("file") < 0)
	    printf("Could not delete file: %s\n", strerror(errno));

A naive translation of this code to Ada is:

	begin
	    unlink("file");
	exception
	    when posix_error =>
		put("Could not delete file: ");
		put(image(get_error_code));
		new_line;
	end;

This translation is incorrect because "image(get_error_code)", which
corresponds to the C code "strerror(errno)" is evaluated after the first
call to "put".  If "put" modifies errno, the wrong error code will be
printed.

In short, passing data around in global variables is dangerous and leads
to unreadable code.  (Try showing the above code without the surrounding
text to your office mate and see how long it takes him/her to spot the
bug.)

As far as I can tell, Ada 95 provides two ways to resolve this problem,
neither of which I entirely like.  One is to encode the error code in the
exception message.  Then we could have:

	function get_error_code(x: exception_occurence) return error_code;

which would translate the exception_message into an error code.  This
seems to be an abuse of the intended purpose of the exception_message,
which is to provide human-readable information on the exception.  In
addition, passing the error code around as a string seems cumbersome and
inefficient.  On the plus side, it is backward compatible with the
existing POSIX.5 binding.

The other approach is to map each error code to a separate exception
and use query functions on exception identities to implement classes.
In this case, one would write:

	exception
	    when occurence : others =>
		if is_system_call_error(exception_identity(occurence)) then
		    -- code to handle exception goes here
		else
		    raise;	-- we aren't interested in this exception.
		end if;
	end;

This approach leads to more complicated code than is ideal, because we
write two tests for the identity of the exception (the "when" clause
and the "if" statement).  In Ada 83 it is possible to determine which
exceptions are handled by a routine by simply scanning the "when" clauses;
here you have to read the "if" statement as well.

Have I missed overlooked any possibilities?
Was this problem (making UNIX bindings to Ada) discussed during the Ada 9X
revision process?
					Kenneth Almquist



  parent reply	other threads:[~1995-03-25  1:50 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-23 23:10 Why no exception hierarchy ? Mogens Jensen
1995-03-24 11:19 ` Michel Gauthier
1995-03-24 21:52   ` Tucker Taft
1995-03-25  1:50   ` Kenneth Almquist [this message]
1995-03-25 12:40     ` David Weller
1995-03-27  5:47       ` Kenneth Almquist
1995-03-25 16:03     ` Larry Kilgallen, LJK Software
1995-03-27 19:23   ` Kenneth Almquist
1995-03-28 16:56     ` Larry Kilgallen, LJK Software
1995-03-29  0:00       ` Kevin F. Quinn
1995-03-30  0:00         ` Kevin F. Quinn
1995-03-31  0:00       ` Kenneth Almquist
1995-04-04  0:00       ` Robert Dewar
     [not found]         ` <1995Apr4.210804.9579@eisner.decus.org>
1995-04-05  0:00           ` Ada means what version by default ? Kevin F. Quinn
1995-04-07  0:00             ` Robert Dewar
1995-04-05  0:00           ` Michael Feldman
1995-04-06  0:00             ` Larry Kilgallen
1995-04-07  0:00               ` Robert Dewar
1995-04-07  0:00               ` Jean D. Ichbiah
1995-04-05  0:00         ` Why no exception hierarchy ? Michael Feldman
1995-04-05  0:00         ` Jean D. Ichbiah
1995-04-05  0:00           ` Garlington KE
1995-04-06  0:00             ` Versions of Ada (was Why no exception hierarchy ?) Larry Kilgallen
1995-04-07  0:00               ` Garlington KE
1995-04-07  0:00               ` Robert Dewar
1995-04-05  0:00           ` Why no exception hierarchy ? Robert A Duff
1995-04-05  0:00             ` Jean D. Ichbiah
1995-04-07  0:00               ` Robert Dewar
1995-04-06  0:00           ` Robert Dewar
1995-04-07  0:00             ` Jean D. Ichbiah
1995-04-07  0:00               ` Robert Dewar
1995-04-06  0:00           ` Robert Dewar
1995-04-07  0:00             ` Jean D. Ichbiah
1995-04-06  0:00           ` Robert Dewar
1995-04-07  0:00             ` Norman H. Cohen
1995-04-07  0:00             ` Garlington KE
1995-04-07  0:00             ` Jean D. Ichbiah
1995-04-07  0:00               ` Robert Dewar
1995-04-05  0:00         ` Is "Ada" 95 or 83? (was: Re: Why no exception hierarchy ?) Theodore Dennison
1995-04-07  0:00           ` Robert Dewar
1995-04-07  0:00       ` Ada means what version by default ? Robert I. Eachus
1995-03-25 18:13 ` Why no exception hierarchy ? Robert Dewar
1995-03-28 18:15   ` Jean D. Ichbiah
1995-03-31  0:00   ` Mats Weber
1995-04-04  0:00     ` Robert Dewar
1995-04-06  0:00       ` Mats Weber
1995-04-07  0:00         ` Kenneth Almquist
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox