11.2 Exceptions

Exceptions are run-time errors or generated errors and are of three different classes, with different origins. The try expression can distinguish between the different classes, whereas the catch expression cannot. They are described in Expressions.

errorRun-time error, for example, 1+a, or the process called erlang:error/1,2
exitThe process called exit/1
throwThe process called throw/1

Table 11.1: Exception Classes.

An exception consists of its class, an exit reason (see Exit Reason), and a stack trace (which aids in finding the code location of the exception).

The stack trace can be be bound to a variable from within a try expression, and is returned for exceptions of class error from a catch expression.

An exception of class error is also known as a run-time error.

The call-stack back trace (stacktrace)

The stack back-trace (stacktrace) is a list of {Module,Function,Arity,Location} tuples. The field Arity in the first tuple can be the argument list of that function call instead of an arity integer, depending on the exception.

Location is a (possibly empty) list of two-tuplesthat can indicate the location in the source code of thefunction. The first element is an atom describing the type ofinformation in the second element. The following items canoccur:

  • file
  • The second element of the tuple is a string (list of characters) representing the filename of the source file of the function.
  • line
  • The second element of the tuple is the line number (an integer > 0) in the source file where the exception occurred or the function was called.


Developers should rely on stacktrace entries only fordebugging purposes.

The VM performs tail call optimization, whichdoes not add new entries to the stacktrace, and also limits stacktracesto a certain depth. Furthermore, compiler options, optimizations andfuture changes may add or remove stacktrace entries, causing any codethat expects the stacktrace to be in a certain order or contain specificitems to fail.

The only exception to this rule is the class error with thereason undef which is guaranteed to include the Module,Function and Arity of the attemptedfunction as the first stacktrace entry.