You might want your
catch block to take some initial corrective action
and then rethrow the exception to an outer try
block (in a calling function). It might rethrow the
same exception, or it might throw a different
one. If it throws a different one, it may want to embed the original
exception inside the new one so that the calling method can
understand the exception history. The
InnerException
property of the new exception retrieves the original exception.
Some exceptions make any sense only in the context in which they were
thrown. This is particularly the case with, for example, the
NullReferenceException,
which may result from bad user input. In cases where you
can't anticipate this by checking input in advance,
you should catch the exception, and rethrow an
ArgumentException
to provide the caller with a better indication of the cause of the
problem.
It can sometimes be a good idea to put a catch handler at the
boundary of a component or design layer, to trap unexpected
exceptions. In this case you might throw a custom
InternalErrorException
signaling the client code that something went wrong within your
component.
You are currently reading a PREVIEW of this book.
Get instant access to over
$1 million worth of books and videos.