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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8bc45126c006dce2,start X-Google-Attributes: gid103376,public From: Chris Sparks Subject: Re: Love's exception handling Date: 1996/09/13 Message-ID: X-Deja-AN: 180691574 sender: Ada programming language comments: Gated by NETNEWS@AUVM.AMERICAN.EDU newsgroups: comp.lang.ada x-mailer: Elm [revision: 112.6] Date: 1996-09-13T00:00:00+00:00 List-Id: 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;