wood burning stoves 2.0*
The moose likes Java in General and the fly likes User defined errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "User defined errors" Watch "User defined errors" New topic
Author

User defined errors

ravin kacha
Greenhorn

Joined: Jul 18, 2007
Posts: 22
can we have User defined Errors like user defined Exception?
if yes How and if no Why?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Both Exception and RuntimeException are non-final classes, so you can just extend them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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.

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Ninad Kulkarni wrote:You can extend java.lang.Error . . .
Agree

But why would you want to?
ravin kacha
Greenhorn

Joined: Jul 18, 2007
Posts: 22
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 13, 2005
Posts: 38016
    
  22
It is obvious why we need application-specific Exceptions, but they are asking about Errors here.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Whoops.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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

Joined: Oct 13, 2005
Posts: 38016
    
  22
We try to answer the question the person asked . . . and the question they ought to have asked
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

Ok Sir
 
 
subject: User defined errors
 
Similar Threads
Can We Apply User Defined Threads in EJB?
Why WSDL2Java says so?
Application of user defined class loader?
servlets: can we override service method ?if yes how and why?if no then why?
Getting various errors after upgrading to 5.0.0