Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Catching Throwable

Thread view: 
Scott Harper - 05 May 2006 17:43 GMT
I got bit by the NoClassDefFoundError.  Lo and behold, I figured out what the
problem was, so thankfully that's not the question here...   :-)

This occurs in a servlet.  In my doPost method, as a last resort, I was
catching (Exception e) just to make sure anything unexpected didn't go
un-noticed.  What was throwing me off was that my last-chance catch block
never got executed to alert me of the problem.  I wasn't getting any error
messages, but things just weren't working.

Alas, NoClassDefFoundError is not and Exception.  By changing my last-chance
catch block to catch (Throwable) instead, I now get notified of the error.

As a relative newcomer to the Java language, is it considered good form to
catch Throwable?

scott
alexandre_paterson@yahoo.fr - 05 May 2006 20:19 GMT
> I got bit by the NoClassDefFoundError.  Lo and behold, I figured out what the
> problem was, so thankfully that's not the question here...   :-)
[quoted text clipped - 10 lines]
> As a relative newcomer to the Java language, is it considered good form to
> catch Throwable?

No... It is usually considered bad form.

But what kind of Error do you plan to catch this way?

If it's only to catch NoClassDefFoundError, then not only it won't
work all the time and moreover there's a better alternative, clearly
explained in Joshua Bloch and Neal Gafter's
"Java Puzzlers" book.  Puzzle 44: "Cutting Class".

A small excerpt:

"In summary, *do not depend on catching NoClassDefFoundError.
"The language specification carefully describes when class
"initialization occurs [JLS 12.4.1], but class loading is far less
"predictable. More generally, *it is rarely appropriate to catch
"Error or its subclasses*. These exceptions are reserved for
"failures from which recovery is not feasible.

A NoClassDefFoundError can be worked around by using
reflection and catching a ClassNotFoundException (which, as
it names implies, is not an Error).

So remember that catching Throwable will not even catch
all NoClassDefFoundError.

That said I still sometimes "catch" Throwable using:

       Thread.setDefaultUncaughtExceptionHandler(
           new Thread.UncaughtExceptionHandler() {
               public void uncaughtException(
                   final Thread t, final Throwable e) {

to do some special kind of logging should "the sh.t hit the fan"
(eg when a 3rd party API which we're forced to use exhibits
some strange behaviour :)

Hope it helps a little bit,

 Alex
Scott Harper - 08 May 2006 18:00 GMT
>> As a relative newcomer to the Java language, is it considered good form to
>> catch Throwable?
>
>No... It is usually considered bad form.
>
>But what kind of Error do you plan to catch this way?

Well that's just it,  I don't know.  But in this specific case just having an
indication that an error occurred would have saved me some time.  This was
also an error condition that would absolutely have been detected prior to
product delivery, so I can understand the argument against in that case.  But
who knows what other undetected/untested problems may be lurking out there?

I've come across the explanation that catching too big of a superclass can
hide or mask serious errors that you weren't expecting.

However, I've also seen a couple places that say it is appropriate in certain
instances, such as on a server application, or when you absolutely don't want
to crash.

>If it's only to catch NoClassDefFoundError, then not only it won't
>work all the time and moreover there's a better alternative, clearly
[quoted text clipped - 16 lines]
>So remember that catching Throwable will not even catch
>all NoClassDefFoundError.

I was already catching Exception, but the ClassNotFoundException wasn't being
thrown...  I'm not too familiar with reflection, guess I'll have to come up to
speed on it.

scott
James McGill - 08 May 2006 18:21 GMT
> However, I've also seen a couple places that say it is appropriate in
> certain
> instances, such as on a server application, or when you absolutely
> don't want
> to crash.

Another way of looking at this, is if you've caught an undeclared
throwable (e.g., runtime exception) you've *already* crashed.
Chris Uppal - 09 May 2006 10:57 GMT
> I've come across the explanation that catching too big of a superclass can
> hide or mask serious errors that you weren't expecting.

This is true, but like all rules you shouldn't apply it blindly.

> However, I've also seen a couple places that say it is appropriate in
> certain
> instances, such as on a server application, or when you absolutely don't
> want
> to crash.

I'd say that there are circumstances where it is appropriate to catch
Throwable, but that they are not the same as "you absolutely don't want to
crash".  There is no guarantee that a Throwable exception is recoverable, and
it may be that the best you can hope for is to provide better diagnostics than
a stack trace written to the console (if the application is even connected to
one).  The other reason why it might be "correct" to catch throwable is if the
application is stuctured in separate "sessions" (in some general sense) where
problems in one session should not (if at all possible) disrupt other sessions.
So, for instance, a server might catch and log Throwable exceptions in each
session, and attempt to close the problematic session without affecting any
others.  It might be that there was no way to recover, but if so then catching
Throwable wouldn't make the inevitable crash any worse, and it might allow it
to continue after unanticipated, but not /globally/ fatal errors.

   -- chris
Sebastian Millies - 09 May 2006 16:52 GMT
Am Tue, 9 May 2006 10:57:36 +0100 schrieb Chris Uppal:

> others.  It might be that there was no way to recover, but if so then catching
> Throwable wouldn't make the inevitable crash any worse, and it might allow it
> to continue after unanticipated, but not /globally/ fatal errors.
>
>     -- chris

Isn't that what happens by default when one starts a new thread in a
multithreaded application? I believe the default behavior of the jvm
is to print a stack trace of any uncaught exception when it exits the
run-Method, and not affect any other running threads. Is that correct?
-- Sebastian
Chris Uppal - 10 May 2006 09:57 GMT
[me:]
> > others.  It might be that there was no way to recover, but if so then
> > catching Throwable wouldn't make the inevitable crash any worse, and it
> > might allow it to continue after unanticipated, but not /globally/
> > fatal errors.
[,,,]
> Isn't that what happens by default when one starts a new thread in a
> multithreaded application? I believe the default behavior of the jvm
> is to print a stack trace of any uncaught exception when it exits the
> run-Method, and not affect any other running threads. Is that correct?

Now you mention it, I believe the JVM does behave that way.  I had forgotten.

Not quite the same thing as I was describing, though, although the ideas are
clearly similar.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.