comp.lang.ada
 help / color / mirror / Atom feed
* Re: Love's exception handling
@ 1996-09-13  0:00 Chris Sparks
  1996-09-16  0:00 ` Robert B. Love 
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Sparks @ 1996-09-13  0:00 UTC (permalink / raw)



I was experimenting with Mr. Love's exception handling example.

This was my first working version.  I then proceeded to clean up this example
and came up with code following after this.

-------------------------------------------------------------------------------
> with Ada.Exceptions; use Ada.Exceptions;
> with Text_io;        use Text_io;
>
> procedure Exception_demo is
>
>   type A_String is access String;
>
>   Error_list: array (1..3) of A_String;
>
>   procedure Square is begin
>
>     raise Constraint_error;
>
>   exception
>     when Error_in_square_function: others =>
>
>       declare
>
>         Eo : Exception_Occurrence;
>         Ei : Exception_Id;
>
>       begin
>         Ada.Exceptions.Save_occurrence (Eo, Error_in_square_function);
>
>         Ei            := Exception_identity (Eo);
>         Error_List(1) := new String'(Exception_name (Ei) & " was hit Square");
>
>         Raise_exception (Ei, Error_List(1).all);
>
>       end;
>
>   end Square;
>
>   procedure Compute is begin
>
>     Square;
>
>   exception
>     when Error_in_compute: others =>
>
>       declare
>
>         Eo : Exception_Occurrence;
>         Ei : Exception_Id;
>
>       begin
>         Ada.Exceptions.Save_occurrence (Eo, Error_in_compute);
>
>         Ei            := Exception_identity (Eo);
>         Error_List(2) := new String'(Exception_name (Ei) & " was hit
 Compute");
>
>         Raise_exception (Ei, Error_List(2).all);
>
>       end;
>
>   end Compute;
>
> begin
>
>   Compute;
>
> exception
>   when Error_in_demo: others =>
>
>     declare
>
>       Eo : Exception_Occurrence;
>       Ei : Exception_Id;
>
>     begin
>       Ada.Exceptions.Save_occurrence (Eo, Error_in_demo);
>
>       Ei            := Exception_identity (Eo);
>       Error_List(3) := new String'(Exception_name (Ei) & " was hit
 Exception_Demo");
>
>     end;
>
>     for I in Error_list'range loop
> --    Text_io.Put_line ("Message    : " & Exception_message
 (Error_list(I).EO));
> --    Text_io.Put_line ("Name       : " & Exception_name
 (Error_list(I).EO));
> --    Text_io.Put_line ("Informaton : " & Exception_information
 (Error_list(I).EO));
>       Text_io.Put_line ("Info       : " & Error_list(I).all);
>     end loop;
>
> end Exception_demo;

-------------------------------------------------------------------------------

I thought the following might be useful to others.  That is why I am posting
this to cla.  It probably can be cleaned up to use internal data structures
instead of a constrained array (like linked lists).

What would be nice, in accordance with a previous post I sent out, to be able
to ascertain the program name without explicitly using a string literal. :-)

This a freebee, so enjoy.  BTW no one should try to copyright this okay :-0

Chris B. Sparks (aka Mr. Ada)
sparks@aisf.com

-------------------------------------------------------------------------------

> with Ada.Exceptions; use Ada.Exceptions;
>
> package Ex is
>
>   type A_String is access String;
>
>   Error_list: array (1..3) of A_String;
>
>   procedure Handler
>     (Target  : in Exception_Occurrence_Access;
>      Index   : in Integer;
>      P_Name  : in String;
>      Elevate : in Boolean);
>
> end Ex;
> --------------------------
> package body Ex is
>
>   procedure Handler
>     (Target  : in Exception_Occurrence_Access;
>      Index   : in Integer;
>      P_Name  : in String;
>      Elevate : in Boolean) is
>
>     Ei : Exception_Id;
>
>   begin
>     Ei                 := Exception_identity (Target.all);
>     Error_List (Index) := new String'(P_Name & ":" & Exception_name (Ei));
>
>     case Elevate is
>       when True  => Raise_exception (Ei, Error_List (Index).all);
>       when False => null;
>     end case;
>
>   end Handler;
>
> end Ex;
> --------------------------
> with Ada.Exceptions; use Ada.Exceptions;
> with Text_io;        use Text_io;
> with Ex;             use Ex;
>
> procedure Exception_demo is
>
>   procedure Square is begin
>
>     raise Constraint_error;
>
>   exception
>     when Error_in_square_function: others =>
>       Ex.Handler (Save_Occurrence (Error_in_Square_Function), 1, "Square",
 True);
>
>   end Square;
>
>   procedure Compute is begin
>
>     Square;
>
>   exception
>     when Error_in_compute: others =>
>       Ex.Handler (Save_Occurrence (Error_in_compute), 2, "Compute", True);
>
>   end Compute;
>
> begin
>
>   Compute;
>
> exception
>   when Error_in_demo: others =>
>     Ex.Handler (Save_Occurrence (Error_in_Demo), 3, "Exception_Demo", False);
>
>     for I in Error_list'range loop
>       Text_io.Put_line ("Info : " & Error_list(I).all);
>     end loop;
>
> end Exception_demo;




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1996-09-20  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-13  0:00 Love's exception handling Chris Sparks
1996-09-16  0:00 ` Robert B. Love 
1996-09-20  0:00   ` Richard Riehle
1996-09-20  0:00   ` Richard Riehle

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