• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

User defined errors

 
ravin kacha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we have User defined Errors like user defined Exception?
if yes How and if no Why?
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both Exception and RuntimeException are non-final classes, so you can just extend them.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can extend java.lang.Error

Following is quote from documentation.

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.



Refer Java Documentation for details.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:You can extend java.lang.Error . . .
Agree

But why would you want to?
 
ravin kacha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But do we need to create this kind of classes?? (I mean User Defined Error classes)
Can anybody give me an example or scenario??


Thanks in advance
Ravin Kacha
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of reasons. Application-specific exceptions can be used to encapsulate and/or encapsulate underlying framework exceptions, providing an easy way to apply declarative exception handling in a robust way, even if the underlying exceptions change. Application-specific exceptions can provide application-specific information in the exception (like a user ID, etc.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is obvious why we need application-specific Exceptions, but they are asking about Errors here.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Agree

But why would you want to?


Hello Campbell
Delay in reply becuase My PC had problem also my internet connection is very very slow so posting of one message takes more time.

Your question is obvious and Java Documentation also mentioned that not try to catch it.

Because Ravin Kacha wanted to know that
"can we have User defined Errors like user defined Exception?"


 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We try to answer the question the person asked . . . and the question they ought to have asked
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic