📎 Webclip
Errors and Exceptions
The chapter separates errors into compile-time mistakes, logical bugs, run-time crashes, and generated exceptions, then focuses on the functional subset of Erlang. It explains common compiler messages, the main run-time errors, and why some failures should be left to crash while others should be returned as tuples or handled with exceptions.
Reading notes#
- Compile-time errors usually come from syntax problems, wrong function arity, mismatched module names, unused variables, or clauses that can never match.
- Logical errors are harder to find because they do not crash the program; tests, TypEr, Dialyzer, debugging, and tracing are suggested tools.
- Common run-time errors include
function_clause,case_clause,if_clause,badmatch,badarg,undef,badarith,badfun,badarity, andsystem_limit. erlang:error/1ends the current process and can produce a stack trace.exit/1also stops the current process, but it is tied to process-level intent and does not return a stack trace.throw/1is used for cases the programmer is expected to handle and can support non-local returns.try ... catchcan handlethrow,error, andexit, with patterns that behave likecaseclauses.afteralways runs, even when an exception is raised, and is mainly useful for side effects such as closing a file.catchis shorter but less precise, because it wraps errors and exits as{'EXIT', Reason}and can hide whether a value is an exception or a normal result.- In the tree example, throws are used to stop a recursive search early when a value is found, avoiding repeated result checks.
