comp.lang.ada
 help / color / mirror / Atom feed
From: des walker <des.walker@gecm.com>
Subject: Re: Exception scope and handling
Date: 2000/11/13
Date: 2000-11-13T00:00:00+00:00	[thread overview]
Message-ID: <3A0FD365.FD581E0@gecm.com> (raw)
In-Reply-To: 8uo87r$ato$1@nnrp1.deja.com

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





      parent reply	other threads:[~2000-11-13  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-13  0:00 Exception scope and handling Sandro Binetti
2000-11-13  0:00 ` John English
2000-11-13  0:00   ` Robert Dewar
2000-11-13  0:00 ` des walker [this message]
replies disabled

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