| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| procedure Test (Rec : Record) is begin ... throw 3 possible exceptions... exception when A | B | C => Print_Test (Rec); end Test; -- Ludovic Brenta. |
|
#3
| |||
| |||
| 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; |
|
#4
| |||
| |||
| 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. |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| 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 |
|
#8
| |||
| |||
| 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 |
|
#9
| |||
| |||
| 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 |
|
#10
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.