comp.lang.ada
 help / color / mirror / Atom feed
From: "Vinzent Hoefler" <0439279208b62c95f1880bf0f8776eeb@t-domaingrabbing.de>
Subject: Re: best practice: error handling
Date: Tue, 31 May 2011 19:13:16 +0200
Date: 2011-05-31T19:13:16+02:00	[thread overview]
Message-ID: <op.vwcvgefflzeukk@jellix.jlfencey.com> (raw)
In-Reply-To: 0b95a2a1-6e3d-4ad1-a832-e3099a9bce37@v8g2000yqb.googlegroups.com

milouz wrote:

> I'm wondering about the best way to handle errors in Ada when calling
> a function/procedure that may fails.

Well, I'd say, there is no "best way". It depends on the application and
the precise context.

> For example, when programming on Unix systems in C, the called
> functions usually return a zero or positive value on success and -1 on
> failure, while setting a global variable (errno) to give some
> informations about the failure.

As rule of thumb I'd suggest the following:

If the error should not occur under (almost) all sane circumstances (for
instance, waiting on a semaphore should always succeed, unless the handle
is invalid), then raise an exception.

If you kind of expect the call to fail under normal conditions, a proper
out-Parameter (like "Success" ;) can be added.

Keep in mind, that returning some error requires a function, and until now
functions can't have "out"-parameters, so returning an "error code" is only
possible for those subroutines which do not need to have "out"-parameters.
So for the sake of having a consistent interface, you might want to to use an
"out"-parameter, regardless if the subroutine in question could otherwise be
expressed as function or not.

On the other hand, if you want a function to return its result, because it
looks more natural, you cannot use an "Success : out Boolean", so it may be
more convenient to raise an exception for errors instead of using an "out"-
parameter for the result.


Vinzent.

-- 
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.



  reply	other threads:[~2011-05-31 17:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-31 14:01 best practice: error handling milouz
2011-05-31 17:13 ` Vinzent Hoefler [this message]
2011-05-31 17:57   ` AdaMagica
2011-06-11 13:38   ` Yannick Duchêne (Hibou57)
2011-05-31 18:08 ` Jeffrey Carter
2011-05-31 20:02   ` Dmitry A. Kazakov
2011-05-31 20:35     ` Jeffrey Carter
2011-05-31 21:40       ` Dmitry A. Kazakov
2011-06-11 13:57     ` Yannick Duchêne (Hibou57)
2011-06-11 14:07       ` Dmitry A. Kazakov
2011-06-12  5:19         ` Randy Brukardt
2011-06-11 13:48   ` Yannick Duchêne (Hibou57)
2011-06-11 13:31 ` Yannick Duchêne (Hibou57)
2011-06-11 16:12   ` Simon Wright
2011-06-11 16:26   ` Dmitry A. Kazakov
replies disabled

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