File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Catching the Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Catching the Error" Watch "Catching the Error" New topic
Author

Catching the Error

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Hi Ranchers ,
Though it is not good to catch Error . why java has provided the facility to catch the Error ??

Thanks in advance .
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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

Joined: Feb 28, 2009
Posts: 212
Thanks Mike

but One more on this :

Is it possible for me to catch the ObjectOutOfMemoryError ?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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

Joined: Feb 28, 2009
Posts: 212
Great explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Catching the Error