aspose file tools*
The moose likes Beginning Java and the fly likes Error (not exception) handling in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error (not exception) handling in java" Watch "Error (not exception) handling in java" New topic
Author

Error (not exception) handling in java

premchand Singh
Greenhorn

Joined: Mar 12, 2012
Posts: 28
Is it possible to handle errors in java( not exception)??
Please give me one or two code for example.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

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


Avinash G.A
OCP Java SE 6 Programmer, OCP Java EE 5 Web Component Developer, OCE Java EE 6 Web Services Developer, VMware Certified Core Spring 3.x Developer, EMC Proven Professional (ISM-V2)
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Jun 09, 2003
Posts: 4632
> Is it possible to handle errors in java

replace the programmer?
premchand Singh
Greenhorn

Joined: Mar 12, 2012
Posts: 28
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

Joined: Apr 10, 2010
Posts: 308
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



Bye,
Nicola
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Nicola Garofalo wrote: . . .
Thatₑs not a Throwable, but a Fallable
Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
You should precede a catch(Throwable) or catch(Error) byLook up the ThreadDeath ← link for more details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error (not exception) handling in java