What "handle an exception" means
A comprehensive guide tohandling
exceptions
inJava
: we'll firstly understand how to decide how tohandle
anexception
, and then we'll discover the three ways tohandle
anexception
: delegate the calling method to do so, completelyhandle
theexception
in the current method, or a mix of these two ways.Useful resources
The video page on YouTube.
The video's slides.
In cases where we do not know how to
handle
anexception
in the current method, we can delegate the calling method, which will then receive theexception
and decide how tohandle
it. In this example we see the case of achecked
exception
.In cases where we do not know how to
handle
anexception
in the current method, we can delegate the calling method, which will then receive theexception
and decide how tohandle
it. In this example we see the case of anunchecked
exception
.An
exception
can be completely handled in the current method; in that case the calling method will never know that anexception
has occurred. In this example we see the case of achecked
exception
.An
exception
can be completely handled in the current method; in that case the calling method will never know that anexception
has occurred. In this example we see the case of anunchecked
exception
and why this is not really the correct way tohandle
anunchecked
exception
.In this example we see that, in the case of an
unchecked
exception
, it is preferable to prevent theexception
from occurring rather than letting it be thrown and then handled bytry
/catch
blocks or by thethrows
keyword.In this example we see that an