Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12926

Re: Exception Handling

From Novice <novice@example..com>
Newsgroups comp.lang.java.programmer
Subject Re: Exception Handling
Date 2012-03-12 22:43 +0000
Organization Your Company
Message-ID <XnsA014BE740A491jpnasty@94.75.214.39> (permalink)
References <XnsA012D42584994jpnasty@94.75.214.39> <4f5e4260$0$293$14726298@news.sunsite.dk>

Show all headers | View raw


Arne Vajhøj <arne@vajhoej.dk> wrote in
news:4f5e4260$0$293$14726298@news.sunsite.dk: 

> On 3/10/2012 8:51 PM, Novice wrote:
>> I've been trying to get my head around exception handling for the
>> last few days concurrently with my efforts to start doing logging
>> correctly. 
>>
>> I've re-read the Java Tutorials section on exception handling. I read
>> Bloch's remarks on Exception Handling in Effective Java (2nd edition)
>> several days ago. I'm working my way through Stelting's Robust Java.
>>
>> It's time to ask some questions to make sure I'm on the right track.
>>
>> I'm struggling with the best way to revise some of my existing code.
>> Let's consider a concrete example and then see if we can generalize
>> to come up with a proper error handling strategy.
>>
>> I have a utility class called LocalizationUtils which basically
>> houses convenience methods dealing with i18n/l10n. One of its methods
>> is getResources(). It expects two parameters, a String representing
>> the "base name" (the leading part of the resource file name) and a
>> locale. 
> 
>> In terms of trouble spots, I know from experience that getResources()
>> will throw a MissingResourceException, which is an unchecked
>> exception, if the base name is misspelled. A null value in either
>> parameter is also going to make the getResources() method fail:
>> ResourceBundle.getBundle() will throw a NullPointerException if
>> either parameter is null. 
>>
>> My objective is to code getResources() and getLocalizedText() as
>> professionally as possible. If the methods fail, I want to write a
>> clear message to my log and the log record needs to include a
>> stacktrace. (I'm assuming that a console is not necessarily available
>> to the operators running this program and I need to know where the
>> error took place. Based on an article Arved suggested in another
>> thread, 
>> http://www.javacodegeeks.com/2011/01/10-tips-proper-application- 
>> logging.html, I'm inclined to minimize "position" information - class 
>> name, method name, line number - in the log and instead get all of
>> that from the stacktrace.)
> 
> I would not worry about the performance until you know that you
> have a problem.
> 
>> Since the only exceptions I anticipate, MissingResourceException and
>> NullPointerException, are unchecked exceptions, I gather than I
>> shouldn't really do anything about them aside from logging when they
>> happen. 
> 
> If you only log - then what?
> 
>>                                                                   
>>                              Okay, 
>> fair enough; I certainly don't want to display the source code to my
>> user, make them enter the correct value for the baseName, make them
>> recompile the program and then run it again! But when/where should I
>> log the error? For instance, if I mess up my coding somehow and
>> inadvertently pass a null in the baseName, should getResources() be
>> testing its input parameters individually to see if they are null and
>> write a message to the log if they are null from within that method?
>> I'm inclined to say yes to that but I'm not sure what to do next. It
>> seems pointless to carry on with getResources() since
>> ResourceBundle.getBundle(baseName, locale) will fail on a
>> NullPointerException with a null in either parameter. I could throw a
>> NullPointerException so that getLocalizedText() can deal with it. But
>> how do I get a stacktrace into the log if I follow that strategy?
>> It's easy enough to get the stacktrace once I've caught an exception
>> but I'm just talking about recognizing that an input parameter is
>> null; there's no exception at that point to put in my log. 
>>
>> If I let getResources() throw NullPointerException if either
>> parameter is null, then I can let getLocalizedText() catch those
>> NullPointerExceptions and get the stacktraces from the exception and
>> write them to the log. In that case, I may as well just check the
>> input parameters for nulls, and simply throw NullPointerException
>> with a specific message within getResources(); then
>> getLocalizedText() can have a try/catch block. The try/catch can
>> detect the NullPointerException and write both the message and the
>> stacktrace to the log. 
>>
>> But I gather that you should handle the error as close to where it
>> happened as possible when you can. I'd tend to prefer to handle the
>> null parameter values in getResources() except that I'm not sure how
>> to write the stack trace to the log before any exception has
>> happened. 
> 
> Do you need the stacktrace in that case?
> 

Yes. I call getResources() from several places in the program and just 
knowing getResources() was called doesn't tell me which call had the 
problem. Was it the call in the main menu that launches the program? Was 
it the call in the program itself that sets up the GUI? Was it the call 
in the preferences dialog? etc. So I'm going to have trouble determining 
which bundle was desired unless I know at least the method which called 
getResources(). Now, if getResources() was only invoked once in the life 
of the program, just knowing that it happened would be all I'd need to 
know what it was trying to find. 

>> One other questions. When, if ever, should I execute System.exit()
>> with a non-zero integer?
> 
> When you want to terminate the program with an error status to the
> shell.
>
Okay, thanks.
> 
-- 
Novice

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Exception Handling Novice <novice@example..com> - 2012-03-11 01:51 +0000
  Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-10 18:39 -0800
    Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 11:53 -0300
      Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 10:51 -0700
        Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 16:35 -0300
    Re: Exception Handling Novice <novice@example..com> - 2012-03-11 17:05 +0000
      Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 10:53 -0700
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 20:36 +0000
      Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 11:07 -0700
        Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 17:00 -0300
          Re: Exception Handling Novice <novice@example..com> - 2012-03-11 22:02 +0000
        Re: Exception Handling Arivald <NOSPAMarivald@interia.pl> - 2012-03-11 21:03 +0100
          Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 18:31 -0300
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 21:54 +0000
          Re: Exception Handling Patricia Shanahan <pats@acm.org> - 2012-03-11 15:26 -0700
            Re: Exception Handling Novice <novice@example..com> - 2012-03-11 23:23 +0000
              Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 16:52 -0700
                Re: Exception Handling Novice <novice@example..com> - 2012-03-12 17:16 +0000
                Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-13 08:31 -0700
              Re: Exception Handling Patricia Shanahan <pats@acm.org> - 2012-03-11 17:51 -0700
                Re: Exception Handling Novice <novice@example..com> - 2012-03-12 17:26 +0000
            Re: Exception Handling Arne Vajhøj <arne@vajhoej.dk> - 2012-03-12 14:49 -0400
          Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-11 20:46 -0300
            Re: Exception Handling Novice <novice@example..com> - 2012-03-12 17:43 +0000
      Re: Exception Handling Patricia Shanahan <pats@acm.org> - 2012-03-11 11:14 -0700
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 22:35 +0000
          Re: Exception Handling Lew <noone@lewscanon.com> - 2012-03-11 16:58 -0700
            Re: Exception Handling Novice <novice@example..com> - 2012-03-12 15:44 +0000
              Re: Exception Handling Gene Wirchenko <genew@ocis.net> - 2012-03-12 10:34 -0700
      Re: Exception Handling Arivald <NOSPAMarivald@interia.pl> - 2012-03-11 20:34 +0100
        Re: Exception Handling Novice <novice@example..com> - 2012-03-11 22:36 +0000
    Re: Exception Handling Arne Vajhøj <arne@vajhoej.dk> - 2012-03-12 14:41 -0400
  Re: Exception Handling Arne Vajhøj <arne@vajhoej.dk> - 2012-03-12 14:37 -0400
    Re: Exception Handling Novice <novice@example..com> - 2012-03-12 22:43 +0000
      Re: Exception Handling Lew <lewbloch@gmail.com> - 2012-03-12 16:11 -0700
      Re: Exception Handling Arivald <NOSPAMarivald@interia.pl> - 2012-03-13 00:54 +0100
        Re: Exception Handling Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-13 06:05 -0300

csiph-web