• 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 the Error

 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers ,
Though it is not good to catch Error . why java has provided the facility to catch the Error ??

Thanks in advance .
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the idea that you shouldn't catch an Error isn't always correct. It's often true, but it's not an absolute rule by any means. Sadly, many people seem to think it is an absolute rule. Ignore them.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike

but One more on this :

Is it possible for me to catch the ObjectOutOfMemoryError ?
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is.

It's risky, because it's possible that memory is still very low at the time you catch the error, and so there's a risk that anything you do might cause a new OutOfMemoryError. However, it may well be better to at least try to handle the error. If it doesn't work, well, you're not really any worse off than if you hadn't caught it. And often you can handle such errors fairly reliably. A common situation is: you call a method that performs some operation that needs a lot of memory, but when the method completes, the memory becomes available again. If an OOME is thrown, you probably don't want to catch that inside the method, because the memory is probably still very low. But catching the error outside the method could work quite well, as by the time you catch the error, one or more key reference variables have gone out of scope, and one or more large objects that were previously not available for GC, now are available for GC. Maybe you can try the method again later, when the machine is less busy. If nothing else, you can log the error, which is probably better than doing nothing.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic