The catch or declare principle
The catch or declare principle states that when a method can throw a checked exception, the method must catch the exception with a try-catch statement or declare that it throws the exception in its method signature. This rule ensures that checked exceptions are properly handled or propagated up the call stack so that the calling method can handle them.
Understanding the principle
The catch or declare principle holds for checked exceptions. If a checked exception is not declared or caught, the code won’t compile. For unchecked exceptions, the catch or declare rule does not apply. They are usually caused by programming errors or unexpected situations that cannot be predicted or anticipated. Unchecked exceptions can be caught and handled, but it is not mandatory. Let’s see how we can declare exceptions.
Now that we have seen how to declare exceptions, let’s have a look at how to deal with exceptions with the try-catch statement...