1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2003</year><year>2011</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
24
<title>Errors and Error Handling</title>
29
<file>errors.xml</file>
33
<title>Terminology</title>
34
<p>Errors can roughly be divided into four different types:</p>
35
<list type="bulleted">
36
<item>Compile-time errors</item>
37
<item>Logical errors</item>
38
<item>Run-time errors</item>
39
<item>Generated errors</item>
41
<p>A compile-time error, for example a syntax error, should not
42
cause much trouble as it is caught by the compiler.</p>
43
<p>A logical error is when a program does not behave as intended,
44
but does not crash. An example could be that nothing happens when
45
a button in a graphical user interface is clicked.</p>
46
<p>A run-time error is when a crash occurs. An example could be
47
when an operator is applied to arguments of the wrong type.
48
The Erlang programming language has built-in features for
49
handling of run-time errors.</p>
50
<p>A run-time error can also be emulated by calling
51
<c>erlang:error(Reason)</c> or <c>erlang:error(Reason, Args)</c>
52
(those appeared in Erlang 5.4/OTP-R10).</p>
53
<p>A run-time error is another name for an exception
54
of class <c>error</c>.
56
<p>A generated error is when the code itself calls
57
<c>exit/1</c> or <c>throw/1</c>. Note that emulated run-time
58
errors are not denoted as generated errors here.
60
<p>Generated errors are exceptions of classes <c>exit</c> and
63
<p>When a run-time error or generated error occurs in Erlang,
64
execution for the process which evaluated
65
the erroneous expression is stopped.
66
This is referred to as a <em>failure</em>, that execution or
67
evaluation <em>fails</em>, or that the process <em>fails</em>,
68
<em>terminates</em> or <em>exits</em>. Note that a process may
69
terminate/exit for other reasons than a failure.</p>
70
<p>A process that terminates will emit an <em>exit signal</em> with
71
an <em>exit reason</em> that says something about which error
72
has occurred. Normally, some information about the error will
73
be printed to the terminal.</p>
77
<title>Exceptions</title>
78
<p>Exceptions are run-time errors or generated errors and
79
are of three different classes, with different origins. The
80
<seealso marker="expressions#try">try</seealso> expression
81
(appeared in Erlang 5.4/OTP-R10B)
82
can distinguish between the different classes, whereas the
83
<seealso marker="expressions#catch">catch</seealso>
84
expression can not. They are described in the Expressions chapter.</p>
87
<cell align="left" valign="middle"><em>Class</em></cell>
88
<cell align="left" valign="middle"><em>Origin</em></cell>
91
<cell align="left" valign="middle"><c>error</c></cell>
92
<cell align="left" valign="middle">Run-time error for example <c>1+a</c>, or the process called <c>erlang:error/1,2</c> (appeared in Erlang 5.4/OTP-R10B)</cell>
95
<cell align="left" valign="middle"><c>exit</c></cell>
96
<cell align="left" valign="middle">The process called <c>exit/1</c></cell>
99
<cell align="left" valign="middle"><c>throw</c></cell>
100
<cell align="left" valign="middle">The process called <c>throw/1</c></cell>
102
<tcaption>Exception Classes.</tcaption>
104
<p>An exception consists of its class, an exit reason
105
(the <seealso marker="#exit_reasons">Exit Reason</seealso>),
106
and a stack trace (that aids in finding the code location of
108
<p>The stack trace can be retrieved using
109
<c>erlang:get_stacktrace/0</c> (new in Erlang 5.4/OTP-R10B)
110
from within a <c>try</c> expression, and is returned for
111
exceptions of class <c>error</c> from a <c>catch</c> expression.</p>
112
<p>An exception of class <c>error</c> is also known as a run-time
117
<title>Handling of Run-Time Errors in Erlang</title>
120
<title>Error Handling Within Processes</title>
121
<p>It is possible to prevent run-time errors and other
122
exceptions from causing
123
the process to terminate by using <c>catch</c> or
124
<c>try</c>, see the Expressions chapter about
125
<seealso marker="expressions#catch">Catch</seealso>
126
and <seealso marker="expressions#try">Try</seealso>.</p>
130
<title>Error Handling Between Processes</title>
131
<p>Processes can monitor other processes and detect process
133
the <seealso marker="processes#errors">Processes</seealso>
139
<marker id="exit_reasons"></marker>
140
<title>Exit Reasons</title>
141
<p>When a run-time error occurs,
142
that is an exception of class <c>error</c>,
143
the exit reason is a tuple <c>{Reason,Stack}</c>.
144
<c>Reason</c> is a term indicating the type of error:</p>
147
<cell align="left" valign="middle"><em>Reason</em></cell>
148
<cell align="left" valign="middle"><em>Type of error</em></cell>
151
<cell align="left" valign="middle"><c>badarg</c></cell>
152
<cell align="left" valign="middle">Bad argument. The argument is of wrong data type, or is otherwise badly formed.</cell>
155
<cell align="left" valign="middle"><c>badarith</c></cell>
156
<cell align="left" valign="middle">Bad argument in an arithmetic expression.</cell>
159
<cell align="left" valign="middle"><c>{badmatch,V}</c></cell>
160
<cell align="left" valign="middle">Evaluation of a match expression failed. The value <c>V</c> did not match.</cell>
163
<cell align="left" valign="middle"><c>function_clause</c></cell>
164
<cell align="left" valign="middle">No matching function clause is found when evaluating a function call.</cell>
167
<cell align="left" valign="middle"><c>{case_clause,V}</c></cell>
168
<cell align="left" valign="middle">No matching branch is found when evaluating a <c>case</c> expression. The value <c>V</c> did not match.</cell>
171
<cell align="left" valign="middle"><c>if_clause</c></cell>
172
<cell align="left" valign="middle">No true branch is found when evaluating an <c>if</c> expression.</cell>
175
<cell align="left" valign="middle"><c>{try_clause,V}</c></cell>
176
<cell align="left" valign="middle">No matching branch is found when evaluating the of-section of a <c>try</c> expression. The value <c>V</c> did not match.</cell>
179
<cell align="left" valign="middle"><c>undef</c></cell>
180
<cell align="left" valign="middle">The function cannot be found when evaluating a function call.</cell>
183
<cell align="left" valign="middle"><c>{badfun,F}</c></cell>
184
<cell align="left" valign="middle">There is something wrong with a fun <c>F</c>.</cell>
187
<cell align="left" valign="middle"><c>{badarity,F}</c></cell>
188
<cell align="left" valign="middle">A fun is applied to the wrong number of arguments. <c>F</c> describes the fun and the arguments.</cell>
191
<cell align="left" valign="middle"><c>timeout_value</c></cell>
192
<cell align="left" valign="middle">The timeout value in a <c>receive..after</c> expression is evaluated to something else than an integer or <c>infinity</c>.</cell>
195
<cell align="left" valign="middle"><c>noproc</c></cell>
196
<cell align="left" valign="middle">Trying to link to a non-existing process.</cell>
199
<cell align="left" valign="middle"><c>{nocatch,V}</c></cell>
200
<cell align="left" valign="middle">Trying to evaluate a <c>throw </c>outside a <c>catch</c>. <c>V</c> is the thrown term.</cell>
203
<cell align="left" valign="middle"><c>system_limit</c></cell>
204
<cell align="left" valign="middle">A system limit has been reached. See Efficiency Guide for information about system limits.</cell>
206
<tcaption>Exit Reasons.</tcaption>
208
<p><c>Stack</c> is the stack of function calls being evaluated
209
when the error occurred, given as a list of tuples
210
<c>{Module,Name,Arity}</c> with the most recent function call
211
first. The most recent function call tuple may in some
212
cases be <c>{Module,Name,[Arg]}</c>.</p>