• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error (not exception) handling in java

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to handle errors in java( not exception)??
Please give me one or two code for example.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 78
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicola Garofalo wrote: . . .

Thatₑs not a Throwable, but a Fallable
 
Avinash Ga
Ranch Hand
Posts: 78
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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......
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should precede a catch(Throwable) or catch(Error) byLook up the ThreadDeath ← link for more details.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic