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,fd8f3d2ebf73a4b7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-11-22 01:34:08 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!ppp-1-26.cvx1.telinco.NET!not-for-mail From: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: Whether to raise exception or End_of_List function Date: Thu, 22 Nov 2001 03:53:15 -0000 Message-ID: <9tht9e$2j1ni$4@ID-25716.news.dfncis.de> References: NNTP-Posting-Host: ppp-1-26.cvx1.telinco.net (212.1.136.26) X-Trace: fu-berlin.de 1006401647 2721522 212.1.136.26 (16 [25716]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Xref: archiver1.google.com comp.lang.ada:16837 Date: 2001-11-22T03:53:15+00:00 List-Id: "Preben Randhol" wrote in message news:slrn9vntpg.7nm.randhol+abuse@kiuk0156.chembio.ntnu.no... > ... > As I also read > that exceptions can be a monstrosity if over used. And sometimes a piece of code can become a monstrosity (in terms of a proliferation of if-then-elses or whatever) if exceptions are underused. Indeed, sometimes the neophyte attempting to avoid using the humble 'goto' can end up in a similar mess. When should one use exceptions? Exceptionally. The clue is in the name ;-) -- Best wishes, Nick Roberts Let me provide a bit of an example. Supposing we are reading a sequence of data items from a device. Version A: procedure Read_Data is begin if Status_Bit(Port(1047)) /= 1 then Put(Current_Error,"Could not read altitude first byte"); else Read(Port(1050),Alt_Bytes(1)); if Status_Bit(Port(1047)) /= 1 then Put(Current_Error,"Could not read altitude second byte"); else Read(Port(1050),Alt_Bytes(2)); if Status_Bit(Port(1047)) /= 1 then ... and so on until you run off the right margin! Version B: Read_Port_Error: exception; procedure Read_Byte (Byte: out Interfaces.Unsigned_8) is begin if Status_Bit(Port(1047)) /= 1 then raise Read_Port_Error; end if; Read(Port(1050),Byte); end; procedue Read_Data is begin Read_Byte(Alt_Bytes(1)); Read_Byte(Alt_Bytes(2)); ... Much better, eh? If more detailed diagnostics are required, one can always use Ada.Exceptions and provide extra information in the Message parameter when calling the Raise_Exception procedure.