Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error (not exception) handling in java

 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to handle errors in java( not exception)??
Please give me one or two code for example.
 
Maneesh Godbole
Saloon Keeper
Posts: 11085
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Technically, since Error subclasses Throwable, one can handle it. However, from the API docs,
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.
 
Avinash Ga
Ranch Hand
Posts: 78
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error is an irrecoverable state of the program. So its better not to think of handling one, instead we hunt the root cause itself. If you are pretty much sure that you can handle the un-desirable state, then use Exceptions. That will make your code cleaner.

Best Wishes
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would ask the question why would you even consider handling an error? What are you going to do with it? Like others have said, your application has reached a state you cannot recover from.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Is it possible to handle errors in java

replace the programmer?
 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys try to understand the question ...my intention is not going in depth..
just i wana to know how to handle it..
Like for some error we can write in try catch block if we are pretty sure to error occur..so what are the other suitable method to handle them..
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
premchand Singh wrote:Guys try to understand the question ...my intention is not going in depth..
just i wana to know how to handle it..
Like for some error we can write in try catch block if we are pretty sure to error occur..so what are the other suitable method to handle them..


I think everybody is free to do what she/he want, so here it's the dirty code ;D

 
Campbell Ritchie
Sheriff
Pie
Posts: 49729
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote: . . .
Thatₑs not a Throwable, but a Fallable
 
Avinash Ga
Ranch Hand
Posts: 78
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:
premchand Singh wrote:Guys try to understand the question ...my intention is not going in depth..
just i wana to know how to handle it..
Like for some error we can write in try catch block if we are pretty sure to error occur..so what are the other suitable method to handle them..


I think everybody is free to do what she/he want, so here it's the dirty code ;D



yes.... the code is dirty enough to call it as dirty..... do we ever need to be that much dirty?
ans:- its depends on a guy with an apple in his hands......
 
Winston Gutkowski
Bartender
Pie
Posts: 10490
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
premchand Singh wrote:Guys try to understand the question ...my intention is not going in depth..
just i wana to know how to handle it..
Like for some error we can write in try catch block if we are pretty sure to error occur...

And you've already been given the answer by experts (which is presumably why you came here to ask in the first place).

DON'T - because whatever code you write to do so is likely to be redundant.

...so what are the other suitable method to handle them.

None. Let the program fail.

The only possible exception I can think of might be to enclose your entire app in a try...catch block in its main() method for logging purposes, perhaps something like:but whatever you do, don't "swallow" the Error.

Winston

 
Campbell Ritchie
Sheriff
Pie
Posts: 49729
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should precede a catch(Throwable) or catch(Error) byLook up the ThreadDeath ← link for more details.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic