From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Ada and "early return" - opinion/practice question Date: Tue, 16 Mar 2021 11:46:06 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> <86eegge32o.fsf@stephe-leake.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 16 Mar 2021 10:46:08 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="4d4f3a071be095b96aa2cdcda90f81b9"; logging-data="6671"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+m1ZJ6A7OgK3VKRxawGoZGt4ubBCBKyZE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 Cancel-Lock: sha1:DRFOZsfGd6W2uxujsJQA8GiGEus= In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61559 List-Id: 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 >    > 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 then >    raise > 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 " 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