Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32930
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Way to avoid repeating to catch the same exception in overridden method |
| Date | 2015-10-23 10:32 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <n0d2cl$k3f$1@dont-email.me> (permalink) |
| References | <fd943245-c84e-4b93-9ed5-e18fb25e256a@googlegroups.com> <5629f731$0$23869$e4fe514c@news.xs4all.nl> |
On Fri, 23 Oct 2015 11:00:33 +0200, Silvio wrote: > As others already stated something is fishy if getXXX() without > parameters can throw an exception unless you have defined that getting > XXX when the object does not have an XXX at all is invalid. But in that > case there should also be something to query that, say a hasXXX() or > something similar. Returning null to indicate a missing value might > simply work here as well. > > I think it is a bad idea to try to hide the exception from the user by > filling in empty values. The result will be wrong anyway but neither the > user nor yourself will be alerted. > > At the very least replace the COMPLETE toString result with a value that > indicates that something is wrong and show that to the user. Also a lot > simpler to code. > > But by all means consider throwing the exception if it actually > indicates a program error. If it doesn't then consider not throwing the > exception in the first place. > > +1 Returning null is common in both C and Java under these circumstances and is simple enough to check for. All you need to is to update the method- level comment to say that null can be returned and describe the circumstances when this can occur (your project DOES mandate the use javadocs, doesn't it?). -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Way to avoid repeating to catch the same exception in overridden method mike <mikaelpetterson@hotmail.com> - 2015-10-19 01:02 -0700
Re: Way to avoid repeating to catch the same exception in overridden method Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-19 12:11 -0400
Re: Way to avoid repeating to catch the same exception in overridden method Robert Klemme <shortcutter@googlemail.com> - 2015-10-23 08:59 +0200
Re: Way to avoid repeating to catch the same exception in overridden method Martin Gregorie <martin@address-in-sig.invalid> - 2015-10-19 12:26 +0000
Re: Way to avoid repeating to catch the same exception in overridden method Silvio <silvio@internet.com> - 2015-10-23 11:00 +0200
Re: Way to avoid repeating to catch the same exception in overridden method Martin Gregorie <martin@address-in-sig.invalid> - 2015-10-23 10:32 +0000
csiph-web