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,ea2b3498720dfde1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-28 09:58:12 PST Message-ID: <3DBD7954.80502@cogeco.ca> From: "Warren W. Gay VE3WWG" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Good Ada Style For Impossible Actions? References: <3DBB0508.6010707@acm.org> <8w2v9.20054$T_.485432@iad-read.news.verio.net> <3DBD7066.1040205@acm.org> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 28 Oct 2002 12:52:20 -0500 NNTP-Posting-Host: 198.96.47.195 X-Complaints-To: abuse@sympatico.ca X-Trace: news20.bellglobal.com 1035827541 198.96.47.195 (Mon, 28 Oct 2002 12:52:21 EST) NNTP-Posting-Date: Mon, 28 Oct 2002 12:52:21 EST Organization: Bell Sympatico Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!torn!webster!nf1.bellglobal.com!nf2.bellglobal.com!news20.bellglobal.com.POSTED!not-for-mail Xref: archiver1.google.com comp.lang.ada:30189 Date: 2002-10-28T12:52:20-05:00 List-Id: Jeffrey Carter wrote: > Dr Nancy's Sweetie wrote: > >> Jeffrey Carter wrote, in reply to a question about when to use exceptions >> and functions for testing error conditions: >> >>> It sounds as if you could have mouse events outside the proper window >>> at any time, so such an event would not be exceptional. So having a >>> function would probably be a good idea. >>> >>> But if you use the function and still get a mouse event outside the >>> proper window, you should also have an exception to indicate that. >> >> If the function tries to compute a logical address for an invalid >> mouse position, a CONSTRAINT_ERROR will be raised because the logical >> position will be outside its valid range. >> >> Is there a good heuristic for "raise your own exception" and "don't >> clutter up the exception namespace"? > > The function returns Boolean: True if the mouse pointer is in the active > window; False otherwise. So it should catch the exception and return False. > > The other subprogram returns the mouse position within the active > window, and should only be called if the function mentioned above > returns True. But of course people will ignore that rule, and in any > case it may be possible for the mouse pointer to move between the 2 > calls. If the mouse pointer is outside the active window when this > subprogram is called, then it should raise a specific exception to > indicate this. Constraint_Error is not very informative in this context. I'd suggest that your function (or procedure) return a discriminated record instead: type Window_Status_Kind is ( Not_In_Window, In_Window ); type Return_Type(Kind : Window_Status_Kind) is record case Kind in when Not_In_Window => null; -- no other information available when In_Window => Y : Line_Type; -- Translated window Y coordinate X : Column_Type; -- Translated window X coordinate end case; end record; The nice thing about this approach is that if your code attempts to access Y or X from the returned value when it is inappropriate to do so, the code will fail immediately, at the source of error. Returning the Y,X separately from a Boolean flag (say), leaves you open to abusing Y,X when they are not valid. I agree that an exception is probably overkill for something that is likely to occur with reasonable frequency. Warren. -- Warren W. Gay VE3WWG http://home.cogeco.ca/~ve3wwg