From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,86c750b8474bf6d5 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!npeer.de.kpn-eurorings.net!npeer-ng1.kpn.DE!news.tele.dk!news.tele.dk!small.news.tele.dk!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Sun, 08 Jun 2008 09:47:30 +0300 From: Niklas Holsti User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060628 Debian/1.7.8-1sarge7.1 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: About String References: <484ABED3.8040909@obry.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <484b802a$0$23844$4f793bc4@news.tdc.fi> Organization: TDC Internet Services NNTP-Posting-Host: laku61.adsl.netsonic.fi X-Trace: 1212907562 news.tdc.fi 23844 81.17.205.61:32789 X-Complaints-To: abuse@tdcnet.fi Xref: g2news1.google.com comp.lang.ada:604 Date: 2008-06-08T09:47:30+03:00 List-Id: Chris Moore wrote: > Pascal Obry wrote: > >> Simon Wright a �crit : >> >>> You might want to handle the problem locally: >>> >>> procedure Patati_Patata is >>> begin >>> declare >>> S : String := A_Function_Raising_Constraint_Error; >>> begin >>> -- some code >>> exception >>> when Constraint_Error => >>> -- deal with the problem and carry on .. >>> end; >>> -- .. here >> >> >> This one is wrong. The exception on the declare section can't be >> handled on the local block as it will raised outside of the block. >> >> Pascal. >> > > You are correct. Yes. > I have to say this is an entirely non-intuitive "feature" of the language. I beg to differ. The program should be read as declare begin end; The exception handler is within the "begin..end" block, not on the "declare". (Indenting the "begin..exception..end" structure one level beyond the "declare" would make this more visible, but is not common practice.) > We talk about declare blocks and yet the > scope of the exception handlers doesn't include the declarations! The declared variables are visible in the whole "begin..end" block, including in the exception handler. If the declarations raise an exception, it would be madness to try to handle it within the "begin..end" block, because the handler could try to use un-elaborated (non-existent) variables. The solution is just to add one more "begin..end" block, enclosing the "declare .. begin .. end", and handle the exceptions in this outer "begin..end" block. Or handle the exceptions in the subprogram's "begin..end" block and nest the "declare..begin..end" within the subprograms "begin..end". That said, I admit that I, too, have made this mistake once or twice. But not lately. I wonder if it would be a good idea for Ada to allow an exception handler also in the "declare" part, as follows: -- NOT Ada! declare exception begin exception end; Of course, in this proposal the declaration-exception handler would not have access to the declared variables. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .