Home

catchandreturn

Catchandreturn is a programming pattern in which a function handles potential errors by catching exceptions and returning a value that encodes the outcome, rather than allowing the exception to propagate. In languages that support exceptions, this approach typically yields a result object or pair that indicates success or failure, so callers can handle the outcome without using try/catch blocks at their call sites.

Implementation often involves wrapping risky operations in a try block and returning a discriminated value, such

Use cases include simplifying caller logic when immediate error handling is preferred at the point of detection,

Trade-offs include the risk of swallowing silent failures if callers do not check the encoded result carefully,

See also error handling, exception handling, result types.

as
a
tuple,
a
small
object,
or
a
union
type.
For
example,
a
Python-like
function
might
return
a
pair
like
(True,
result)
on
success
or
(False,
error)
on
failure.
In
languages
with
native
result
or
option
types,
catchandreturn
is
realized
by
returning
a
value
that
encodes
either
a
success
case
with
the
data
or
an
error
case
with
an
error
descriptor.
or
when
exception
propagation
would
complicate
control
flow
or
performance.
It
is
common
in
APIs
that
aim
for
explicit
and
predictable
error
handling,
or
in
functional-style
code
that
favors
explicit
data
flow
over
thrown
exceptions.
and
potential
clutter
from
mixing
control
data
(like
ok/error
flags)
with
business
data.
It
can
also
obscure
stack
traces
and
debugging
information
compared
with
letting
an
exception
propagate.
In
many
ecosystems,
alternatives
such
as
propagating
exceptions,
or
using
dedicated
result
or
option
types,
are
used
depending
on
language
idioms
and
performance
considerations.