Home

rethrow

Rethrow is the act of throwing an exception again after it has been caught in an error-handling routine. The primary purpose is to propagate the error to higher levels of the call stack or to defer handling to a different layer while preserving the original context. Rethrowing is distinct from wrapping or chaining, where a new exception is created and the original is attached as a cause; rethrow typically reuses the same exception object.

Rethrowing is commonly used after performing cleanup or after adding local context that does not resolve the

Different languages treat rethrow and stack traces differently. In Java, rethrowing the caught exception with throw

Common considerations: avoid suppressing exceptions; ensure that any added context is informative; consider whether rethrowing plus

See also: exception handling, exception propagation, exception chaining, error handling best practices.

error.
It
helps
maintain
visibility
of
the
original
fault,
avoids
swallowing
the
error,
and
allows
higher-level
code
to
apply
its
own
handling
policy.
e
preserves
the
original
stack
trace.
Python’s
except-block
semantics
allow
rethrow
via
raise
to
keep
the
original
traceback,
whereas
raise
e
can
replace
it.
In
C#,
the
preferred
form
to
preserve
the
traceback
is
throw;
whereas
throw
ex;
resets
the
trace
to
the
point
of
the
throw.
additional
handling
(like
logging)
is
appropriate,
or
if
the
error
should
be
wrapped
to
convey
higher-level
meaning.