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: John McCabe Newsgroups: comp.lang.ada Subject: Re: Ada and "early return" - opinion/practice question Date: Tue, 16 Mar 2021 11:51:51 +0000 Organization: A noiseless patient Spider Message-ID: References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> <86eegge32o.fsf@stephe-leake.org> <865z1rea0d.fsf@stephe-leake.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="e18ab550129ff764d5cf1e3494dd6cd3"; logging-data="32166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ETrLNJL503TA9A1YlXTYyuLiQ2opBnbQ=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (windows-nt) Cancel-Lock: sha1:tM4ayePFETAB5bvQvjSGNu68TbA= sha1:vczVmyS3bKY6VayLOrdR7ToD5nc= Xref: reader02.eternal-september.org comp.lang.ada:61561 List-Id: Stephen Leake writes: > John McCabe <"john@nospam.mccabe.org.uk"> writes: > >> On 15/03/2021 17:31, Stephen Leake wrote: >>>John McCabe writes: >>> >> <..snip..> >>.. <..snip..> >>>I would tend to use an exception for "something bad", but that depends >>>on the overall design. >> 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 > > No, your example fn returns a status code to the caller for "something > bad", so the Ada fn should raise an exception, and the caller (or some > function up the call chain) should handle it. >> Or do you mean, rather than return when the undesirable condition occurs, do >> something like: >> if then >> raise >> end if; > Yes, but with helpful info in the exception message: > raise with "explanation"; That "with" part's new since I regularly used Ada :-) > For example, if the problem is that some number is out of bounds, the > "explanation" should include both the number and the bounds. And if the > bounds can be adjusted by a user option, the option name. That seems reasonable; similar to C++ where you can "throw std::logic_error("message");" kind of thing.