comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: About String
Date: Sun, 08 Jun 2008 09:47:30 +0300
Date: 2008-06-08T09:47:30+03:00	[thread overview]
Message-ID: <484b802a$0$23844$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <UJD2k.71707$%B6.16259@newsfe13.ams2>

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 <some variables>
     begin <do something with those variables> 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 <some variables>
    exception <handle exceptions raised in the declarations>
    begin <some statements>
    exception <handle exceptions raised in the statements>
    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
       .      @       .



  reply	other threads:[~2008-06-08  6:47 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-06 17:29 About String Sébastien Morand
2008-06-07 16:18 ` Simon Wright
2008-06-07 17:01   ` Pascal Obry
2008-06-07 22:13     ` Chris Moore
2008-06-08  6:47       ` Niklas Holsti [this message]
2008-06-08  7:35         ` Dmitry A. Kazakov
2008-06-08 10:29           ` Sebastien Morand
2008-06-08 10:53             ` Dmitry A. Kazakov
2008-06-08 11:14           ` Niklas Holsti
2008-06-08 13:16             ` Dmitry A. Kazakov
2008-06-08 17:17               ` Niklas Holsti
2008-06-09  7:26                 ` Dmitry A. Kazakov
2008-06-08 11:48           ` Martin
2008-06-08 13:17             ` Conditional declarations (was: About String) Dmitry A. Kazakov
2008-06-08 18:26           ` About String Chris Moore
2008-06-08 18:32         ` Robert A Duff
2008-06-08 20:51           ` Maciej Sobczak
2008-06-08 21:19             ` Robert A Duff
2008-06-09  7:14               ` Dmitry A. Kazakov
2008-06-09  9:43                 ` Georg Bauhaus
2008-06-09 10:25                   ` Dmitry A. Kazakov
2008-06-09 10:42                     ` Sébastien Morand
2008-06-09 11:43                     ` Georg Bauhaus
2008-06-09 12:03                       ` Dmitry A. Kazakov
2008-06-15 19:38                 ` Robert A Duff
2008-06-15 20:52                   ` Dmitry A. Kazakov
2008-06-15 22:06                     ` Robert A Duff
2008-06-16  8:31                       ` Dmitry A. Kazakov
2008-06-16 19:17                         ` Robert A Duff
2008-06-16 20:30                           ` Dmitry A. Kazakov
2008-06-16 22:02                           ` Georg Bauhaus
2008-06-16 23:04                             ` Robert A Duff
2008-06-09 11:00               ` Georg Bauhaus
2008-06-09 14:27                 ` Britt Snodgrass
2008-06-15 19:50                   ` Robert A Duff
2008-06-15 19:48                 ` Robert A Duff
2008-06-08 11:13     ` Simon Wright
2008-06-08 19:03       ` Sebastien Morand
replies disabled

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