Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare an exception not having to be handled?

Tags:

java

exception

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)

like image 339
stefan.at.wpf Avatar asked Jan 28 '26 07:01

stefan.at.wpf


2 Answers

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.

like image 175
nicholas.hauschild Avatar answered Jan 30 '26 22:01

nicholas.hauschild


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.

like image 21
Ankur Shanbhag Avatar answered Jan 30 '26 20:01

Ankur Shanbhag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!