Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #446
| From | David Lamb <dalamb@cs.queensu.ca> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: When to throw exceptions and when to use System.err? |
| References | <87bp0w8gd4.fsf@merciadriluca-station.MERCIADRILUCA> <8vbe38Fcc2U1@mid.individual.net> <87wrjjw513.fsf@merciadriluca-station.MERCIADRILUCA> |
| Message-ID | <2ojkp.63$g56.35@newsfe04.iad> (permalink) |
| Date | 2011-03-29 07:29 -0400 |
On 28/03/2011 1:04 PM, Merciadri Luca wrote: > Nigel Wade<nmw-news@ion.le.ac.uk> writes: >> There really cannot be any definitive answer. For example, suppose your >> code has already run for an hour, calculating some intermediate results, >> and then asks the user for further input to clarify what to do next >> based on those results. Would it be appropriate at that time to >> completely waste an hour of processing simply because the user input an >> incorrect value? If the code is being used as part of a GUI, and the >> input comes from a text field, an error message output to System.err >> will probably never be seen by the user, and exiting would almost >> certainly be inappropriate. > Thanks for this message. This is really well explained and looks > skillful. > > In the case that presently concerns me these days (because of the > current project), I'm directly parsing CLI args, and the program takes > no more than 2 secs to launch and quit. As a result, in these > conditions, I find inconsistent to throw an exception when, say, an > argument (which had to be some integer) is something else than an > integer, when I do not throw an exception if this argument does not > fulfill some specific rules such as the one which states that its > value needs to be greater than 0. However, it appears natural to throw > an exception in the case of something which is not an integer, because > Integer.parseInt can directly be embedded in a try block, when the > catch follows. So, why *not* throw an exception when the argument is less than zero? Throwing an exception means the method can't proceed normally for some reason; I don't see why you *wouldn't* report "unexpected negative number" as an exception. You need to throw a specific enough exception that the code that calls your method can distinguish among cases that matter to it, and provide enough information for the caller to report something sensible to a user if that's what the caller decides to do. So: - NumberFormatException, thrown by ParseInt, makes sense for the "not really a number" problem, and you can just pass it through. - You need to figure out an appropriate exception to use for "number not in the right range". If you want to go whole hog you could define your own exception that included the value read and the upper/lower bounds you were looking for (e.g. 1 and Integer.MAX_VALUE), which would be plenty for a caller to use in producing a fully informative and localized user message (which you could even provide via your exception class's getLocalizedMessage method). Or you might find some existing exception to be good enough.
Back to comp.lang.java.help | Next — Next in thread | Find similar | Unroll thread
Re: When to throw exceptions and when to use System.err? David Lamb <dalamb@cs.queensu.ca> - 2011-03-29 07:29 -0400
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-29 11:21 -0400
Re: When to throw exceptions and when to use System.err? David Lamb <dalamb@cs.queensu.ca> - 2011-03-29 18:38 -0400
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-29 18:53 -0400
Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-03-30 11:48 -0700
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-30 23:49 -0400
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-30 23:52 -0400
Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-03-31 06:55 -0700
Re: When to throw exceptions and when to use System.err? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-03-31 15:58 +0100
Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-03-31 09:02 -0700
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-31 19:57 -0400
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-31 19:50 -0400
Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-04-01 10:44 -0700
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-04-01 19:52 -0400
Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-04-04 08:47 -0700
Re: When to throw exceptions and when to use System.err? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-04-04 13:40 -0400
Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-04-04 11:56 -0700
Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-04-06 12:40 -0400
csiph-web