Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: Way to avoid repeating to catch the same exception in overridden method Date: Fri, 23 Oct 2015 10:32:53 +0000 (UTC) Organization: A noiseless patient Spider Lines: 35 Message-ID: References: <5629f731$0$23869$e4fe514c@news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 23 Oct 2015 10:32:53 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="20591"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3k5lX0K1t0mGsuQSJ38d2+Z2y7oRRpKM=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:ohQu7AQQe5daeWQcFtI4bw7IQBs= Xref: csiph.com comp.lang.java.programmer:32930 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 |