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


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

Re: Exception Handling

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: Exception Handling
References <XnsA012D42584994jpnasty@94.75.214.39> <4f5e4260$0$293$14726298@news.sunsite.dk> <XnsA014BE740A491jpnasty@94.75.214.39> <jjm2fu$191$1@news.dialog.net.pl>
Message-ID <X5E7r.36034$L12.15480@newsfe23.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2012-03-13 06:05 -0300

Show all headers | View raw


On 12-03-12 08:54 PM, Arivald wrote:
> W dniu 2012-03-12 23:43, Novice pisze:
>> Arne Vajhřj<arne@vajhoej.dk>  wrote in
>>
>>> 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
> 
> Instead of stack trace, you may be more interested in resource ID
> program try to load and failed. If You fix missing resource, it will
> work in all places.
> 
> Just add resource name and locale data to log message.
> 
Agreed. In the general case, simply make sure that your log statements
print enough stuff. It's much less useful, than you might think, to know
who the caller was; the main reason people believe it's important is
because they forgot to log enough data and so are desperate to somehow
recover that by guessing from the calling context.

Static analysis as part of debugging will tell you the potential
callers. If it's really important to know what did call, hey, just log
the potential calling sites also: you have a log statement in
getResources() and a statement in each of the N methods that use
getResources().

Keep stack traces for errors. Rely on a flow of single-line log entries
to help you trace a flow of execution. It's uniform, easier to analyze
with a variety of tools. Logging is cheap this way. No harm done if you
add log statements; you can switch them on and off by class or package
as needed at various levels. If you log like this the only stack traces
that you see belong to real errors.

Make your life easier by enhancing your messages. slf4j helps out with
this by providing argument placeholders, but it's easy to do this
yourself with java.text.MessageFormat. Keep your log message format
strings in a properties file, and consider a utility class that loads
these, and can be accessed at logging time with a property key and the
log message arguments. This utility class uses MessageFormat to do the
work, and returns a fully formatted message to the site of the log.

Consider all the extra identifiers in a message that help trace a flow
of execution: thread ID, logged-in user in a web application etc. Use
PatternLayout (log4j) or similar, or other helper methods
(getAuthenticatedUser() in your utility class) to simplify this framing.

HTH,
AHS

-- 
Last week I helped my friend stay put. It's a lot easier'n helpin' 'em
move. I just went over to his house and made sure that he did not start
to load shit into a truck.
-- Mitch Hedberg

Back to comp.lang.java.programmer | Previous | NextPrevious 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