• 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

catching Throwable

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello if and OutOfMemoryError is thrown the jvm and application stops ?
I would like to know what would be the result of catching Throwable in the case where the bloc of code between try catch cause the jvm to throw a OutOfMemoryError ?

Thanks for explanation.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it and see.

There was somebody on these boards about 2 months ago who had that problem with a large application with millions of objects in, and he managed to keep it running by setting a few million to null, so the garbage collector could delete them. If you don't manage something like that, your JVM will probably still go down.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your answer, i actually wanted to know, what would be the purpose and the result of catching unchecked exceptions and errors having try catch bloc catching Throwable. Usually errors or unchecked exceptions are unrecoverable so what would be the result by catching it we let the application still running. I m working on a multithreaded application and i wasnt sure wether to catch Exceptions or Throwable or just checked exceptions.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably best to stick to checked Exceptions and unchecked Exception (eg NumberFormatException) when you expect them.
Errors and Throwables are only rarely recoverable from, and most unchecked Exceptions which aren't expected (eg NullPointerException, ArrayIndexOutOfBoundsException) suggest an error in the code.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most common reason is probably to log it, perhaps adding some useful information, and maybe rethrowing it afterwards, especially if it's an Error.

When you do this, you can't be sure that any of the code in the catch block will really execute, because it's possible that the problem is so bad that your code will immediately throw another error and leave the catch block. But I usually figure it's better to try to log the problem and fail, rather than not even try.

Specific exceptions and errors may have specific workarounds (yes even Errors, sometimes at least) so you can catch them and handle them - but in this case you probably shouldn't catch Throwable or catch Error, but rather catch the specific type of Throwable that you have a workaround for.

Whenever you're catching an Error or Throwable (including Error), you should think carefully about the consequences. Generally for beginners you simply should not do this. But with more experience you may be able to determine if circumstances warrant catching the error. Generally you should consider: what's the worst thing that can happen if you don't catch it? What's the worst that can happen if you do catch it? Often letting the JVM crash is best thing to do, and restart it - either immediately, or after you've analyzed and fixed the problem.

Note also that letting an Error go uncaught does not necessarily stop the JVM, especially if there are multiple threads involved. And sometimes it may not even get printed to the screen, depending on the environment you're using. Try putting throw new Error("test") in your code to see what effect it has, if you're not sure.

As for catching unchecked exceptions, that's much more reasonable. It's true that those often (not always) signal a programmer error that a programmer will have to fix. But for many applications, it's still reasonable to keep running the program. Let's say you're processing a payroll for all employees, and a few of the employees have left their middle name blank (perhaps they don't have a middle name), causing a NullPointerException. Should you not process any employees because some throw exceptions? I think probably you should try to process all of them, and fix problems for the remaining employees as soon as possible. For example:

[ March 03, 2008: Message edited by: Jim Yingst ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic