Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RuntimeExceptions and CheckedExceptions

What are you views on using CheckedExceptions and RuntimeExceptions in an application ? I've been advised to use a combination of both and, as far as I understand, you can have a chain of CheckedException calls being propagated up along with a RuntimeException.

like image 539
James P. Avatar asked Nov 28 '22 20:11

James P.


1 Answers

Checked exceptions should only be thrown if you can reasonably expect the caller to handle them. Otherwise throw a RuntimeException (which doesn't require that you declare it or that the handler catch it. This is the approach that Spring JDBC takes).

More details from Sun here.

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

like image 142
Joel Avatar answered Dec 04 '22 23:12

Joel