Home

gotounntaksbehandling

Gotounntaksbehandling refers to the practice of using goto statements to manage error handling and resource cleanup in programming. The term combines the keyword goto with unntaksbehandling (exception handling) and is discussed in historical and practical contexts within Norwegian and broader programming literature.

In languages without built-in structured exception handling, developers sometimes jump to a centralized error-handling or cleanup

The technique is especially associated with C and other low-level languages where exceptions are absent or

Alternatives and modern practice favor structured exception handling (try/catch/finally) or explicit result types (such as Result

block
using
goto.
The
common
pattern
performs
a
sequence
of
operations
and,
on
any
failure,
transfers
control
to
a
cleanup
section
that
releases
resources
and
returns
an
error
code.
While
this
approach
can
reduce
duplication
of
cleanup
code,
it
creates
non-linear
control
flow
and
can
lead
to
code
that
is
difficult
to
read
and
maintain,
sometimes
described
as
spaghetti
code.
avoided
for
performance
reasons.
A
typical
pattern
is
to
use
goto
cleanup
as
soon
as
an
error
is
detected.
An
example
might
be:
function()
{
Resource*
r
=
acquire();
if
(r
==
NULL)
return
ERROR;
if
(doStep1(r)
!=
0)
goto
cleanup;
if
(doStep2(r)
!=
0)
goto
cleanup;
int
result
=
OK;
cleanup:
if
(r)
release(r);
return
result;
}
or
Either)
to
separate
error
handling
from
main
logic.
Resource
management
patterns
like
RAII
in
C++
or
the
use
of
smart
pointers
reduce
the
need
for
goto-based
cleanup.
When
goto
is
used,
it
is
typically
restricted
to
a
single
cleanup
label
to
minimize
complexity
and
improve
readability.