Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #453
| From | Lew <noone@lewscanon.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: When to throw exceptions and when to use System.err? |
| Date | 2011-03-29 18:53 -0400 |
| Organization | albasani.net |
| Message-ID | <imtnsf$opf$1@news.albasani.net> (permalink) |
| References | (1 earlier) <8vbe38Fcc2U1@mid.individual.net> <87wrjjw513.fsf@merciadriluca-station.MERCIADRILUCA> <2ojkp.63$g56.35@newsfe04.iad> <imstcp$tg1$1@news.albasani.net> <5ctkp.30$ua4.10@newsfe10.iad> |
David Lamb wrote: > Lew wrote: >> David Lamb wrote: >>> - 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 >> You could also subtype 'IllegalArgumentException' or >> 'NumberFormatException' to distinguish the range problem: > Thanks for pointing that out -- I'd certainly have looked for something to > subclass. I'd want to think about what parent class to use, though. Neither of > those seems quite right for an incorrect user input, if your method is popping > up a dialog box itself. If you're talking about a string passed to your method > that happened to come from user input -- well, there's no way for your method > to know that, so it's an illegal argument (there's nothing wrong with the > format). Sorry, I can't agree that using exceptions to process user input is a good idea. Runtime exceptions are to catch and repair programming errors, not user input. There is no such thing as "bad" input. Therefore no input should ever thrown an exception - not from the business perspective. Yes, exceptions happen low down in the code - presumably only checked exceptions, but that's another debate. But the code that invokes such a method, especially because the checked exception requires it, will catch and deal with the exception. As touted in /Effective Java/ by Joshua Bloch, Chapter 9, [1] you should favor prevention of exceptions over trapping them. To process input, sometimes you have to cheat and use an exception, but by and large you want to validate input prior to submitting it to business logic. The cheat with an exception is a highly-localized idiom to substitute for a more proper conditional, wrapped to pretend to higher layers that it really was a conditional, used only at pressing need. Therefore, no exception, much less the ones I mentioned, is at all "right for an incorrect user input". Runtime exceptions are for things like incorrect arguments to methods or improper data fed to range-bound methods (a kind of incorrect argument). So I figure that in the great majority of applications I'd use 'IllegalArgumentException' for such a thing. [1] <http://java.sun.com/docs/books/effective/toc.html> Item 57: Use exceptions only for exceptional conditions Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors Item 60: Favor the use of standard exceptions *Item 62: Document all exceptions thrown by each method* *Item 65: Don't ignore exceptions* -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.help | Previous | Next — Previous in thread | 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