From: Chris Sparks <sparks@AISF.COM>
Subject: Re: Love's exception handling
Date: 1996/09/13
Date: 1996-09-13T00:00:00+00:00 [thread overview]
Message-ID: <INFO-ADA%96091315275250@LISTSERV.NODAK.EDU> (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;
next reply other threads:[~1996-09-13 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-09-13 0:00 Chris Sparks [this message]
1996-09-16 0:00 ` Love's exception handling Robert B. Love
1996-09-20 0:00 ` Richard Riehle
1996-09-20 0:00 ` Richard Riehle
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox