411 REFERENCE · Wikibooks
Computer Programming/Error handling
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
__TOC__
function √ (X : in Float) : Float
begin
if (X < 0) :
return -1
else
calculate root from x
fi
end
C := √ (A2 + B2)
if C < 0 then
error handling
else
normal processing
fi
function √ (
X : in Float;
Success : out Boolean ) : Float
begin
if (X < 0) :
Success := False
else
calculate root from x
Success := True
fi
end
C := √ (A2 + B2, Success)
if not Success then
error handling
else
normal processing
fi
function √ (X : in Float) : Float
begin
if (X < 0) :
Float_Error := true
else
calculate root from x
fi
end
Float_Error := false
C := √ (A2 + B2)
if Float_Error then
error handling
else
normal processing
fi
function √ (X : in Float) : Float
begin
if (X < 0) :
raise Float_Error
else
calculate root from x
fi
end
try:
C := √ (A2 + B2)
normal processing
when Float_Error:
error handling
yrt
function √ (X : in Float) : Float
pre-condition (X >= 0)
post-condition (return >= 0)
begin
calculate root from x
end
C := √ (A2 + B2)
Language overview
This list gives an overview of the standard or primary behavior and error handling techniques used in various programming languages. This does not mean that other techniques or behavior are not possible in the programming languages named.
+ Error handling in programming languagesLanguageTechniquenull/ 0arrayintfloattype Ada September 2005 exceptions/DbC1 handled handled handled handled handled handled C return/var crash crash fault fault2 fault fault2 C++ exceptions crash crash fault fault2 fault fault2 Python exceptions handled handled handled handled handled handled SPARK DbC handled handled handled handled handled handled Error Types null null pointer or nil element access. / 0 division by 0. array out of bound array access. int out of bound integer range/overflow. float out of bound floating point calculations. type out of bound type conversions. Handled by handled the error is handled by the language in a proper manner - for example with the use of error or exception handlers. fault the program continues to run in an undefined or faulty manner. crash program crashes. Technique DbC The Language uses Design by Contract to avoid error condition. exceptions Language raises an exception or uses a similar technique when an error condition appears. return Language uses an return code to indicate an error condition. var Language uses one or more global variables to indicate an error condition.
1 : Ada supports a very limited form of DbC through its strong typing system.
2 : In C and C++ the behavior is defined and sometimes intentionally used however in this comparison we consider unintentional use.