File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we catch errors in java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we catch errors in java code" Watch "Can we catch errors in java code" New topic
Author

Can we catch errors in java code

Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48
Hi,

Can we catch error in java code like, can we catch OutOfMemoryError, ThreadDeath etc.

Thanks,
Ram
kurt hanni
Ranch Hand

Joined: Aug 11, 2007
Posts: 140

Yes you can, Error is a sublass of Throwable.


SCJA, SCJP, SCJD, SCMAD, SCWCD, SCBCD, SCDJWS, SCEA, SOA, Spring Certified, OCP MySQL 5 Developer, PMP
Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48
Thanks,
Can you please give any one error which we cannot handle
kurt hanni
Ranch Hand

Joined: Aug 11, 2007
Posts: 140

All error objects can be handled. they are all subclass of Error which is a subclass of Throwable.

Remember you must avoid handling errors.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

And there is nothing much you can do even you catch an Error.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ram Chhabra
Ranch Hand

Joined: Jan 07, 2008
Posts: 48
Thanks a lot for clarifications
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Vijitha Kumara wrote:And there is nothing much you can do even you catch an Error.

Depends on the error. A NoClassDefError means a class available at compile time is no longer available. A UnsatisfiedLinkError means a native library cannot be used (it can't be found, or it's missing a dependency). You can catch both and decide to use a workaround.

But I agree that catching errors should be done with very much caution. And OutOfMemoryErrors should never be caught at all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we catch errors in java code