This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes User defined exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "User defined exceptions" Watch "User defined exceptions" New topic
Author

User defined exceptions

sridhar lakka
Ranch Hand

Joined: Jan 02, 2007
Posts: 109
Hi,
Thanks in advance.
In java we can create our own exception. And Java exceptions are mainly divided into two Checked and Unchecked if so, the exception which is created by own will be what type (checked/unchecked).

Regards,
Sree
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can choose.

If your new exception class extends java.lang.Exception, it is a checked exception. In general, your user-defined exceptions should be checked exceptions.

If your new exception class extends java.lang.RuntimeException, it is an unchecked exception. Unchecked exceptions are generally for situations that cannot occur, when the code is working properly - i.e. for indicating bugs.

If your new exception class extends java.lang.Error, it is an Error. Errors are for fatal system problems (e.g. out-of-memory) and are unchecked; there are few valid reasons to define a new Error, but it is legal to do so, if you find a reason.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
If the Exception you create extends RuntimeException (directly or indirectly, eg "WrongCalculationException extends ArithmeticException" counts as RuntimeException because ArithmeticException already extends RuntimeException) = unchecked.

If the Exception you create extends any other sort of Exception and doesn't extend RuntimeException = checked. Example: EthernetWireMissingException extends IOException doesn't extend RuntimeException directly or indirectly.
Another example: SridharsNewException extends Exception = checked.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Peter's description of when to use checked and unchecked fits Sun's intentions when they invented them. That's a perfectly good way to operate and probably good advice for now. But be aware that not everyone agrees and you may well run into applications written with unchecked exceptions. If you find some code like that, read it carefully and see if you think it worked out better or worse in the long run.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: User defined exceptions
 
Similar Threads
Exceptions Query
Userdefined Exception
Use of Checked and Unchecked Exception
How to recognise a checked or unchecked exception
Exception Doubt