File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "User defined exceptions" Watch "User defined exceptions" New topic

User defined exceptions

sridhar lakka
Ranch Hand

Joined: Jan 02, 2007
Posts: 109
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).

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

Joined: Oct 13, 2005
Posts: 46337
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
I agree. Here's the link:
subject: User defined exceptions
It's not a secret anymore!