me.at.coding
me.at.coding

Reputation: 17654

Declare an exception not having to be handled?

In Java, when I decide to throw an exception, is it possible to define this Exception as not having to be handled?

Thanks :-)

(I am on Java 7)

Upvotes: 3

Views: 64

Answers (2)

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

It can be achieved by throwing a unchecked exception. RuntimeException are unchecked exception which the calling program need not handle. Any sub-class like ClassCastException etc, are derived from RuntimeException and you need not worry about handling them.

Upvotes: 1

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

Any Exception that is a child type of RuntimeException is not required to be handled. This is called an unchecked exception.

That said, you can still choose to handle it, should you feel that it is necessary.

Upvotes: 8

Related Questions