tags:

views:

46

answers:

3

If I declare class

Class MyOwnException extends Exception
{
}

Is this is a checked or unchecked exception?

+1  A: 

All exceptions in Java are checked. This means that must be explicitly catched in a try-catch block.

Runtime exceptions need not be caught (java.lang.RuntimeException). The same applies for errors (java.lang.Error).

Therefore your Exception is checked. If you want to make it unchecked, subclass RuntimeException.

kgiannakakis
+2  A: 

If you extend Exception then it is "checked", i.e if you throw it, it must be caught or declared in the method signature.

Unchecked exceptions extend RuntimeException and do not need to be declared or caught. It is also possible to create an unchecked exception by extending Error or one of its subclasses, but these exceptions are by convention reserved for use by the JDK.

Dean Povey
+1  A: 

The exception you have shown is a checked exception and must either be caught or declared as thrown.

The alternative would be an un-checked exception, which is declared as follows:

Class MyOwnException extends RuntimeException
{
  ...
}

While it's not strictly adhered to, extending RuntimeException is meant to be the reserve of the JDK.

Nick Holt
Spring wraps all exceptions to RuntimeExceptions. The issue about checked versus unchecked exceptions is somehow controversial.
kgiannakakis
@kgiannakakis - 'somehow controversial' to say the least :-)
Nick Holt