comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Ada and "early return" - opinion/practice question
Date: Tue, 16 Mar 2021 11:46:06 +0100	[thread overview]
Message-ID: <s2q29g$6gf$1@dont-email.me> (raw)
In-Reply-To: <s2pq0h$gh7$1@dont-email.me>

On 3/16/21 9:24 AM, John McCabe wrote:
> 
> Thinking back on this point, would you do it a style where you try to do
> what needs to be done then handle the exception when it fails? For example,
> in this style
> 
> begin
>     <try to do a good thing>
> exception
>     when blah blah
> end

That depends on a lot of things. In some cases, Bad_Condition means that 
Do_Good_Things will produce a wrong answer rather than fail, so this construct 
cannot be used.

If Do_Good_Things will raise an exception for every Bad_Condition, then it 
depends on how fine-grained those exceptions are and how helpful you want to be 
to the caller. If a different exception is raised for each Bad_Condition, then 
this is equivalent to explicitly raising an exception for each Bad_Condition. If 
you get the same exception for all, you may reduced to telling your caller that 
there's something wrong with the values supplied.

> Or do you mean, rather than return when the undesirable condition occurs, do
> something like:
> 
> if <some undesirable condition 1> then
>     raise <some exception 1>
> end if;
> 
> In other words, does each undesirable condition, when it occurs, throw a
> [potentially] different exception?

I lean toward using fine-grained exceptions, so I'd probably use a different 
exception for each, and document when each exception is raised, or at least 
provide a different exception msg for each.

On the other hand, these kinds of tests can often become preconditions in Ada 
12, in which case you get a single exception, possibly with a generic msg that 
the precondition failed.

Why would you write "raise <some exception 1>" and then talk about "throwing"?
There's no throwing in Ada. That only happens in error-prone languages.

-- 
Jeff Carter
"If you think you got a nasty taunting this time,
you ain't heard nothing yet!"
Monty Python and the Holy Grail
23

  parent reply	other threads:[~2021-03-16 10:46 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 16:46 Ada and "early return" - opinion/practice question John McCabe
2021-03-15 17:02 ` Dmitry A. Kazakov
2021-03-15 17:29   ` John McCabe
2021-03-16  7:08   ` Randy Brukardt
2021-03-15 17:31 ` Stephen Leake
2021-03-15 17:43   ` John McCabe
2021-03-15 18:15     ` Shark8
2021-03-15 20:39       ` Simon Wright
2021-03-15 20:56         ` Chris Townley
2021-03-16  7:19           ` Stéphane Rivière
2021-03-16 10:31             ` Jeffrey R. Carter
2021-03-16  8:28           ` John McCabe
2021-03-16 20:34         ` Simon Wright
2021-03-17  8:05           ` John McCabe
2021-03-17 11:43             ` Simon Wright
2021-03-18  8:08               ` John McCabe
2021-03-18 16:27                 ` Stephen Leake
2021-03-20 13:41                   ` John McCabe
2021-03-15 19:05     ` Paul Rubin
2021-03-16  8:38       ` John McCabe
2021-03-16  9:03     ` Stephen Leake
2021-03-16  9:21       ` John McCabe
2021-03-16  8:24   ` John McCabe
2021-03-16  9:13     ` Stephen Leake
2021-03-16 11:51       ` John McCabe
2021-03-16  9:46     ` Dmitry A. Kazakov
2021-03-16 10:46     ` Jeffrey R. Carter [this message]
2021-03-17  8:18       ` John McCabe
2021-03-17 10:06         ` AdaMagica
2021-03-15 18:12 ` Shark8
2021-03-15 18:20   ` John McCabe
2021-03-15 19:08   ` Paul Rubin
2021-03-15 19:37     ` Shark8
2021-03-16  7:17     ` Randy Brukardt
2021-03-16  9:26       ` Paul Rubin
2021-03-16  9:53       ` Dmitry A. Kazakov
2021-03-16  9:16     ` Stephen Leake
2021-03-16 11:04       ` Niklas Holsti
2021-03-16 22:49         ` Stephen Leake
2021-03-15 18:37 ` Jeffrey R. Carter
2021-03-15 18:54   ` John McCabe
replies disabled

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