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,bc745b8412f53f25 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-02-28 10:34:26 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!martinkl.dialup.fu-berlin.DE!not-for-mail From: Martin Klaiber Newsgroups: comp.lang.ada Subject: Re: Error-names. Date: Sat, 28 Feb 2004 19:31:56 +0100 Organization: Freie Universitaet Berlin Sender: Martin Klaiber Message-ID: References: NNTP-Posting-Host: martinkl.dialup.fu-berlin.de (130.133.237.205) X-Trace: news.uni-berlin.de 1077993264 56110943 F 130.133.237.205 (10182) X-Orig-Path: not-for-mail User-Agent: tin/1.5.12-20020427 ("Sugar") (UNIX) (Linux/2.4.24 (i586)) Xref: archiver1.google.com comp.lang.ada:5932 Date: 2004-02-28T19:31:56+01:00 List-Id: Marius Amado Alves wrote: > First, embrace long names. The standard C expression is (I think) "error > code", so go on and just write Constraint_Error_Code, General_Error_Code, etc. Hey, thanks, that sounds good. > Second, I'm not sure I understand your tentative code Well, in reality it's a library to calculate income tax, which is, of course written in Ada but shall get a C-Interface now. The 'problem' is that I have loads of procedures and functions to set and get the parameters (just counted, it's 35), but not all of them are valid for every year, etc. So I need return-values for the C-Interface, as C doesn't understand the Ada-exceptions. > but maybe I understand what you want. You want to handle all > exceptions in one place, the handling being returning the > corresponding error code. Yes, because I have about 5 or more exceptions which I want to convert into a return-value and everyone of the 35 functions needs this. I am not concerned about the writing but it's easy to make mistakes (later for instance, when new code is added) if so much code is just repeated. So, what I have now is like this: function Set_Parameter_A (A : A_Type) return C_Integer is begin ... -- set the parameter return 0; -- if the setting was ok. exception -- if the parameter is not defined for this year: when Not_Defined => return Not_Defined_Error; -- if the parameter is out of range which is also year-dependent: when Out_Of_Range => return Out_Of_Range_Error; -- if something else went wrong setting the parameter: when Constraint_Error => return Constraint_Error_Code; ... and so on. when others => return General_Error; end Set_Parameter_A; Well, and the same for Set_Parameter_B, and so on, 35 times :-( Ok, I could just copy and paste but I don't like the idea really. Well, in reality the functions have names like Set_ALTER1, Set_HINZUR or Set_RE4 but this should not be important here. The obscure names are the official names from the tax-laws. > Well, the place has to be in a function that contains all the calls that may > raise the exceptions: > function Main return Error_Code_Type is > -- exception-raising calls here > exception > when E : others => return Error_Code_Of (E); > end; > Now you only have to implement Error_Code_Of. > (Ain't top-down design great? ;-) > The 'trick' you could be missing is the "E : others" construct above. E is > the exception occurence. From it you can go to the exception name. See > package Ada.Exceptions--or, better yet, section 11.4 of the ARM. Well, if I understand it right, I still have to distinguish between the different exceptions before I call Error_Code_Of, is this right? Like this (assume I have 5 exceptions A to E): function Main return Error_Code_Type is -- exception-raising calls here exception when A : others => return Error_Code_Of (A); when B : others => return Error_Code_Of (B); when C : others => return Error_Code_Of (C); when D : others => return Error_Code_Of (D); when E : others => return Error_Code_Of (E); end; But now, how does, for instance, Set_Parameter_A does get the return value from Main? Calling function Set_Parameter_A... begin ... exception when others => return Main; raise; end Set... doesn't work, as I already mentioned. Martin