This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 3816

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: 19649
    
  18

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
 
Consider Paul's rocket mass heater.
 
subject: Can we catch errors in java code
 
Similar Threads
Catch array values from html file
Exception
Exceptions
why to remember names of specific exceptions??
How to catch root exception thrown by domain runtime MBean Server in Weblogic