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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8af02d07133bfac9 X-Google-Attributes: gid103376,public From: des walker Subject: Re: Exception scope and handling Date: 2000/11/13 Message-ID: <3A0FD365.FD581E0@gecm.com>#1/1 X-Deja-AN: 693029155 Content-Transfer-Encoding: 7bit References: <8uo87r$ato$1@nnrp1.deja.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Trace: 13 Nov 2000 11:36:37 GMT, farwkn6911.frlngtn.gecm.com Organization: Alenia-Marconi Systems MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-11-13T00:00:00+00:00 List-Id: Sandro Binetti wrote: > There's something that's not clear, or, better, I can't understand > about exception scope and handling. > > Suppose to declarate an exception inside the declarative region of a > procedure, and handle it at the end of the body of the procedure. > What's the meaning of re-raising this exception outside this body? > > Take a simple example: > > procedure PROC1 is > > procedure PROC2 is > FOO:EXCEPTION; > begin > .... > .... > EXCEPTION > when FOO => handle_it; > raise; -- ???? what's the meaning of this > end proc2; > > begin > ... > ... > ... > -- what kind of object is FOO here? > EXCEPTION > when others => -- ???? why coul'd I handle FOO here, even if I don't > -- know anything about it? > end proc1; > > -- > Ciao, Sandro > > Sent via Deja.com http://www.deja.com/ > Before you buy. Hi Sandro, according to my copy of Programming in Ada (Barnes) what you show here is valid - the exception can be handled outside of its scope. by using the 'others' clause. This is because exceptions can be thought of as existing throughout the life of the program rather than existing dynamically only when you hit the declaration at runtime. Barnes goes on to show that an exception can be propogated out of scope and then back into scope as it is passed up the calling chain. viz: package Odd_Exception is procedure A; end Odd_Exception; package body Odd_Exception is I : Integer := 10; procedure B is begin A; exception when others => raise; end B; procedure A is At_Zero : exception; begin I := I-1; if I > 0 then B; else raise At_Zero; end if; exception when At_Zero => Text_Io.Put_Line("detected At_Zero exception"); raise; end A; end Odd_Exception; naturally this is not a very useful example! But if the procedure A is invoked it will call procedure B, which in turn calls procedure A, 9 times before the At_Zero exception is raised. The exception is propogated through each call of procedure B and procedure A. It is handled by name in each call of procedure A so that the text will be output 10 times (even though the exception was not in scope when reraised in procedure B). I have no idea what use you could actually find for this feature, but maybe others will know :-) Regards Des Walker Alenia-Marconi Systems