Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: About java program. Date: Sat, 06 Jul 2013 13:01:56 +0200 Lines: 81 Message-ID: References: <324f929f-772e-4004-81e1-6b454cdae794@googlegroups.com> <19wnriq53qxel.u9rt8wy6z52g.dlg@40tude.net> <1lvu4yv8sxe73$.n4o05ixuag9y.dlg@40tude.net> <85GdncWZdo04-lzMnZ2dnUVZ8mydnZ2d@bt.com> <51d6396c$0$295$14726298@news.sunsite.dk> <51d77b0d$0$290$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net NHOgi3qgYBORTaOik8xBbwUrbGuGhjYSJbtS/2NwIgTraYXwo= Cancel-Lock: sha1:SnqTBnmaS7y+tB5mB51mznm4kk4= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 In-Reply-To: <51d77b0d$0$290$14726298@news.sunsite.dk> X-Antivirus: avast! (VPS 130705-1, 05.07.2013), Outbound message X-Antivirus-Status: Clean Xref: csiph.com comp.lang.java.programmer:24676 On 06.07.2013 04:03, Arne Vajh=F8j wrote: > On 7/5/2013 5:15 PM, Robert Klemme wrote: >> On 05.07.2013 10:13, lipska the kat wrote: >>> private boolean askYesNoquestion (String str) throws >>> BadArgumentException{ >>> >>> boolean result =3D false; >>> >>> if(null =3D=3D str){ >>> logger.log("Argument is null"); >>> throw new BadArgumentException("Argument is null"); >>> } >>> else{ >>> if(str.equals("yes")){ >>> result =3D true; >>> } >>> else if(!str.equals("no")){ >>> logger.log("Incorrect Argument, argument is " + str); >>> throw new BadArgumentException("argument is " + str); >>> } >>> } >>> return result; >>> } >>> >>> This is *one* way of doing it >>> >>> Overkill ... no, defensive yes. >> >> This example demonstrates what I will call bad practice: the exception= >> is thrown so the calling code can decide how to handle the error. It >> may actually be that the caller can perfectly deal with that situation= >> and proceed normally. Method askYesNoquestion() cannot know this. Bu= t >> because the method does the logging itself you'll end up seeing messag= es >> in a log file for things which are not noteworthy. I say, either log >> and deal with the error locally OR throw an exception containing a >> meaningful message - but not both. That is misleading. > > I do not agree with the last point. > > If we look at the matrix: > > caller logs caller do not log > log insignificant extra log entry fine I do not agree to the "insignificant". > not log fine big problem troubleshooting > > then it seems obvious to me to log anyway. Worst case by logging > is not nearly as bad as worst case not logging. Maybe I haven't made myself clear enough: it is the task of the writer=20 of the calling code to decide whether the exception is a problem or not. = This decision cannot be done by the author of this method. Presumably = this is a low level method which is invoked from many different places=20 and so some may be able to handle the exception while some may not.=20 Since a RuntimeException (NPE is one) will be likely caught somewhere=20 high up the call chain and logged there (usually with the stack trace)=20 debugging should not be too difficult. If on the other hand you need to = wade through tons of log messages which do not signal an error then=20 debugging is also harmed. Not to mention the additional IO overhead for = all the superfluous log messages. Cheers robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/