• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Throwable and Exception

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When we try to use the catch block, will there be any difference if we call like catch(Throwable e) or catch(Exception e) ?

Can there be a case where catch(Throwable e) will be called but not catch(Exception e) ?

Thanks
Madhu
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Error will not be catched if you use catch Exception
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that :
(from API)
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

If you're building a reasonable application, you should then avoid catching a Throwable, but catch Exception instead.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.



I've always considered this suspect with at least one good exception. Say my application lets a user enter the size of a matrix they want to work with. They enter a big number and I get an OutOfMemoryError trying to allocate the matrix. Should I let the application fall over and spew a trace stack on the console? Or catch the error and say "I don't have enough memory for such a big matrix".

I have seen this kind of recovery from OutOfMemoryError in my Wiki server but I didn't recall catching Throwable anywhere, so I just had a look through old logs. I found that the reflection API caught the OutOfMemoryError and threw an InvocationTargetException.

Anybody ever felt the need to catch OutOfMemory or StackOverflow?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I've sometimes caught Errors, including OutOfMemoryError. Just because you threw an OOME in the middle of some resource-intensive method doesn't mean you can't recover from it outside that method. I'll also sometimes catch Error or Throwable. Typically I do this to log & rethrow, e.g.

or

Here unknown unchecked exceptions are ultimately rethrown, but with usefulContextualInfo added. This might well include info like: what record number were we working on at the time, what were values of other local variables which might be ofinterest when debugging, etc. Any error that I don't already understand the cause of, I want to make it easy to diagnose later. So that contextual info can be very helpful.
[ May 12, 2006: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OutOfMemoryError has the implication that the VM is "out of memory". This is not true - merely that a malloc failed. Hence, "out of memory" is recoverable as it is and always has been in native machine code.

The point is, if Sun says something is right, question it - since often, the inverse is true.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's why you should avoid catching Throwable unless you exactly know what you're doing: http://www.javatuning.com/why-catch-throwable-is-evil-real-life-story
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic