• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we catch errors in java code

 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks,
Ram
 
kurt hanni
Ranch Hand
Posts: 140
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can, Error is a sublass of Throwable.

 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
Can you please give any one error which we cannot handle
 
kurt hanni
Ranch Hand
Posts: 140
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there is nothing much you can do even you catch an Error.
 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for clarifications
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic