Common exception handling

This is a discussion on Common exception handling within the ADA forums in Programming Languages category; I'm wondering if there is a clean way to do something common for all throw exceptions and then handle each exception. For exception... procedure Test (Rec : Record) is begin ... throw 3 possible exceptions... exception when A => Print_Test (Rec); ... when B => Print_Test (Rec); ... when C => Print_Test (Rec); ... when others => Print_Test (Rec); end Test; I'd like to not duplicate the Print_Test in each situation. Is there an easy way Thanks, Shaun...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-26-2008, 08:17 AM
shaunpatterson@gmail.com
Guest
 
Default Common exception handling

I'm wondering if there is a clean way to do something common for all
throw exceptions and then handle each exception. For exception...

procedure Test (Rec : Record) is
begin

... throw 3 possible exceptions...


exception
when A =>
Print_Test (Rec);
...
when B =>
Print_Test (Rec);
...
when C =>
Print_Test (Rec);
...
when others =>
Print_Test (Rec);
end Test;

I'd like to not duplicate the Print_Test in each situation. Is there
an easy way

Thanks,

Shaun
Reply With Quote
  #2  
Old 08-26-2008, 08:37 AM
Ludovic Brenta
Guest
 
Default Re: Common exception handling

procedure Test (Rec : Record) is
begin
... throw 3 possible exceptions...
exception
when A | B | C => Print_Test (Rec);
end Test;

--
Ludovic Brenta.
Reply With Quote
  #3  
Old 08-26-2008, 08:39 AM
Martin
Guest
 
Default Re: Common exception handling

On Aug 26, 1:17*pm, shaunpatter...@gmail.com wrote:
[snip]
> I'd like to not duplicate the Print_Test in each situation. *Is there
> an easy way


Just add an extra level of exception handling and re-raise the
exception when you've done the common bit:

with Ada.Text_IO; use Ada.Text_IO;

procedure Throw_Three is

type Record_Type is
record
I : Integer;
end record;

procedure Print_Test (Rec : Record_Type) is
begin
Put_Line ("Print_Test:" & Integer'Image (Rec.I));
end Print_Test;

A, B, C : exception;

procedure Test (Rec : Record_Type) is
begin
begin
case Rec.I is
when 1 => raise A;
when 2 => raise B;
when 3 => raise C;
when others => raise Program_Error;
end case;
exception
when others =>
Print_Test (Rec); -- Common bit
raise; -- Re-raise the same exception
end;
exception
when A =>
Put_Line ("A");
when B =>
Put_Line ("B");
when C =>
Put_Line ("C");
when others =>
Put_Line ("others");
end Test;
begin
for I in 1 .. 4 loop
Test (Rec => (I => I));
end loop;
end Throw_Three;
Reply With Quote
  #4  
Old 08-26-2008, 08:48 AM
Ludovic Brenta
Guest
 
Default Re: Common exception handling

Martin wrote:
> exception
> when A =>
> Put_Line ("A");
> when B =>
> Put_Line ("B");
> when C =>
> Put_Line ("C");
> when others =>
> Put_Line ("others");


If all you want it to print the name of the exception, it is better
like this:

exception
when E : others =>
Put_Line (Ada.Exceptions.Exception_Name (E));

--
Ludovic Brenta.
Reply With Quote
  #5  
Old 08-26-2008, 08:58 AM
Martin
Guest
 
Default Re: Common exception handling

On Aug 26, 1:48*pm, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
[snip]
> If all you want it to print the name of the exception, it is better
> like this:
>
> exception
> * *when E : others =>
> * * * Put_Line (Ada.Exceptions.Exception_Name (E));
>
> --
> Ludovic Brenta.


Yes, it was just a method of demonstrating the different paths that
have been exercised.

Cheers
-- Martin
Reply With Quote
  #6  
Old 08-26-2008, 09:47 AM
shaunpatterson@gmail.com
Guest
 
Default Re: Common exception handling

Yeah, it looks like the extra layer of exception handling is the only
way. I was hoping I could avoid that if possible.

Thanks,

--
Shaun
Reply With Quote
  #7  
Old 08-26-2008, 10:43 AM
Adam Beneschan
Guest
 
Default Re: Common exception handling

On Aug 26, 6:47 am, shaunpatter...@gmail.com wrote:
> Yeah, it looks like the extra layer of exception handling is the only
> way. I was hoping I could avoid that if possible.


No, I don't think it's the only way. I'm surprised no one has
suggested this:

exception
when E : others =>
Print_Test (Rec);
declare
use Ada.Exceptions;
begin
if Exception_Identity(E) = A'Identity then
... handling for A
elsif Exception_Identity(E) = B'Identity then
... handling for B
etc.
else
... handling for other exceptions you didn't expect,
... but you certainly need to aware that it could
... happen
raise; --maybe
end if;
end;

-- Adam

Reply With Quote
  #8  
Old 08-26-2008, 11:10 AM
Dmitry A. Kazakov
Guest
 
Default Re: Common exception handling

On Tue, 26 Aug 2008 07:43:20 -0700 (PDT), Adam Beneschan wrote:

> On Aug 26, 6:47 am, shaunpatter...@gmail.com wrote:


>> Yeah, it looks like the extra layer of exception handling is the only
>> way. I was hoping I could avoid that if possible.

>
> No, I don't think it's the only way. I'm surprised no one has
> suggested this:
>
> exception
> when E : others =>
> Print_Test (Rec);
> declare
> use Ada.Exceptions;
> begin
> if Exception_Identity(E) = A'Identity then
> ... handling for A
> elsif Exception_Identity(E) = B'Identity then
> ... handling for B
> etc.
> else
> ... handling for other exceptions you didn't expect,
> ... but you certainly need to aware that it could
> ... happen
> raise; --maybe
> end if;
> end;


Hmm, it looks quite ugly, IMO. Exception handler (case-like) is better
structured, cleaner, and possibly more efficient.

---------
If exceptions were first-class citizens allowed in case statements then:

exception
when E : others =>
Print_Test (Rec);
case E is -- This is not Ada, alas!
when A => ...
when B => ...
when others => ...
end case;

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Reply With Quote
  #9  
Old 08-26-2008, 11:18 AM
Jean-Pierre Rosen
Guest
 
Default Re: Common exception handling

shaunpatterson@gmail.com a écrit :
> Yeah, it looks like the extra layer of exception handling is the only
> way. I was hoping I could avoid that if possible.
>

I guess this is what you want...

exception
when others =>
-- common part
...
begin
raise; -- Reraises current exception
exception
when A =>
...
when B =>
...
end;
end;

--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
Reply With Quote
  #10  
Old 08-26-2008, 12:49 PM
Adam Beneschan
Guest
 
Default Re: Common exception handling

On Aug 26, 8:10 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Tue, 26 Aug 2008 07:43:20 -0700 (PDT), Adam Beneschan wrote:
> > On Aug 26, 6:47 am, shaunpatter...@gmail.com wrote:
> >> Yeah, it looks like the extra layer of exception handling is the only
> >> way. I was hoping I could avoid that if possible.

>
> > No, I don't think it's the only way. I'm surprised no one has
> > suggested this:

>
> > exception
> > when E : others =>
> > Print_Test (Rec);
> > declare
> > use Ada.Exceptions;
> > begin
> > if Exception_Identity(E) = A'Identity then
> > ... handling for A
> > elsif Exception_Identity(E) = B'Identity then
> > ... handling for B
> > etc.
> > else
> > ... handling for other exceptions you didn't expect,
> > ... but you certainly need to aware that it could
> > ... happen
> > raise; --maybe
> > end if;
> > end;

>
> Hmm, it looks quite ugly, IMO. Exception handler (case-like) is better
> structured, cleaner, and possibly more efficient.
>
> ---------
> If exceptions were first-class citizens allowed in case statements then:
>
> exception
> when E : others =>
> Print_Test (Rec);
> case E is -- This is not Ada, alas!
> when A => ...
> when B => ...
> when others => ...
> end case;


Maybe this use of CASE should be allowed, even if we don't go all the
way and make exceptions first-class entities in other ways. What does
anyone else think... would it be a worthwhile feature to propose
adding? Or is it just too "special" (if we added this, others would
ask, well why shouldn't we allow CASE on a string, or a record, or a
container, or just expand it using some user-specified relational
operator so that we can use it for everything...)?

-- Adam
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:36 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.