Erlang Reference Manual Errors And Error Handling
Contents |
Top of chapter Character Set Source File Encoding Data Types Top of chapter Terms Number Atom Bit Strings and Binaries Reference Fun Port Identifier Pid Tuple Map List String Record Boolean Escape
Erlang Try Catch Example
Sequences Type Conversions Pattern Matching Top of chapter Pattern Matching Modules Top erlang catch exception of chapter Module Syntax Module Attributes Comments module_info/0 and module_info/1 functions Functions Top of chapter Function Declaration Syntax error handler in compiler design Function Evaluation Tail recursion Built-In Functions (BIFs) Types and Function Specifications Top of chapter The Erlang Type Language Types and their Syntax Type Declarations of User-Defined Types Type Information in
Erlang Exception Handling Example
Record Declarations Specifications for Functions Expressions Top of chapter Expression Evaluation Terms Variables Patterns Match Function Calls If Case Send Receive Term Comparisons Arithmetic Expressions Boolean Expressions Short-Circuit Expressions List Operations Map Expressions Bit Syntax Expressions Fun Expressions Catch and Throw Try Parenthesized Expressions Block Expressions List Comprehensions Bit String Comprehensions Guard Sequences Operator Precedence Preprocessor Top of chapter File Inclusion
Erlang Trap Exit
Defining and Using Macros Predefined Macros Macros Overloading Flow Control in Macros -error() and -warning() directives Stringifying Macro Arguments Records Top of chapter Defining Records Creating Records Accessing Record Fields Updating Records Records in Guards Records in Patterns Nested Records Internal Representation of Records Errors and Error Handling Top of chapter Terminology Exceptions Handling of Run-time Errors in Erlang Exit Reasons Processes Top of chapter Processes Process Creation Registered Processes Process Termination Message Sending Links Error Handling Monitors Process Dictionary Distributed Erlang Top of chapter Distributed Erlang System Nodes Node Connections epmd Hidden Nodes C Nodes Security Distribution BIFs Distribution Command-Line Flags Distribution Modules Compilation and Code Loading Top of chapter Compilation Code Loading Code Replacement Running a Function When a Module is Loaded Ports and Port Drivers Top of chapter Ports Port Drivers Port BIFs 11 Errors and Error Handling 11.1 Terminology Errors can roughly be divided into four different types: Compile-time errors Logical errors Run-time errors Generated errors A compile-time error, for example a syntax error, does not cause much trouble as it is caught by the com
and Processes Links A link is a specific kind of relationship that can be created between two processes. When that relationship is set up and one of the processes what is error handling in java dies from an unexpected throw, error or exit (see Errors and Exceptions), the
Erlang Raise
other linked process also dies. This is a useful concept from the perspective of failing as soon as possible to erlang rethrow stop errors: if the process that has an error crashes but those that depend on it don't, then all these depending processes now have to deal with a dependency disappearing. Letting them die and http://erlang.org/doc/reference_manual/errors.html then restarting the whole group is usually an acceptable alternative. Links let us do exactly this. To set a link between two processes, Erlang has the primitive function link/1, which takes a Pid as an argument. When called, the function will create a link between the current process and the one identified by Pid. To get rid of a link, use unlink/1. When one of the linked http://learnyousomeerlang.com/errors-and-processes processes crashes, a special kind of message is sent, with information relative to what happened. No such message is sent if the process dies of natural causes (read: is done running its functions.) I'll first introduce this new function as part of linkmon.erl: myproc() -> timer:sleep(5000), exit(reason). If you try the next following calls (and wait 5 seconds between each spawn command), you should see the shell crashing for 'reason' only when a link has been set between the two processes. 1> c(linkmon). {ok,linkmon} 2> spawn(fun linkmon:myproc/0). <0.52.0> 3> link(spawn(fun linkmon:myproc/0)). true ** exception error: reason Or, to put it in picture: However, this {'EXIT', B, Reason} message can not be caught with a try ... catch as usual. Other mechanisms need to be used to do this. We'll see them later. It's important to note that links are used to establish larger groups of processes that should all die together: chain(0) -> receive _ -> ok after 2000 -> exit("chain dies here") end; chain(N) -> Pid = spawn(fun() -> chain(N-1) end), link(Pid), receive _ -> ok end. This function will take an integer N, start N processes linked one to the other. In order to be able to
2.1 Terms 2.2 Number 2.3 Atom 2.4 Bit Strings and Binaries 2.5 Reference 2.6 Fun 2.7 Port Identifier http://erlangworld.web.fc2.com/man/reference_manual/part.html 2.8 Pid 2.9 Tuple 2.10 List 2.11 String 2.12 Record 2.13 Boolean 2.14 Escape Sequences 2.15 Type Conversions 3 Pattern Matching 3.1 Pattern Matching 4 Modules 4.1 http://daniel-azuma.com/blog/2016/05/25/family-ties-5-exceptionally-similar Module Syntax 4.2 Module Attributes 4.3 Comments 5 Functions 5.1 Function Declaration Syntax 5.2 Function Evaluation 5.3 Tail recursion 5.4 Built-In Functions, BIFs 6 Expressions 6.1 Expression Evaluation error handling 6.2 Terms 6.3 Variables 6.4 Patterns 6.5 Match 6.6 Function Calls 6.7 If 6.8 Case 6.9 Send 6.10 Receive 6.11 Term Comparisons 6.12 Arithmetic Expressions 6.13 Boolean Expressions 6.14 Short-Circuit Boolean Expressions 6.15 List Operations 6.16 Bit Syntax Expressions 6.17 Fun Expressions 6.18 Catch and Throw 6.19 Try 6.20 Parenthesized Expressions 6.21 Block Expressions 6.22 erlang reference manual List Comprehensions 6.23 Bit String Comprehensions 6.24 Guard Sequences 6.25 Operator Precedence 7 Macros 7.1 Defining and Using Macros 7.2 Predefined Macros 7.3 Flow Control in Macros 7.4 Stringifying Macro Arguments 8 Records 8.1 Defining Records 8.2 Creating Records 8.3 Accessing Record Fields 8.4 Updating Records 8.5 Records in Guards 8.6 Records in Patterns 8.7 Internal Representation of Records 9 Errors and Error Handling 9.1 Terminology 9.2 Exceptions 9.3 Handling of Run-Time Errors in Erlang 9.4 Exit Reasons 10 Processes 10.1 Processes 10.2 Process Creation 10.3 Registered Processes 10.4 Process Termination 10.5 Message Sending 10.6 Links 10.7 Error Handling 10.8 Monitors 10.9 Process Dictionary 11 Distributed Erlang 11.1 Distributed Erlang System 11.2 Nodes 11.3 Node Connections 11.4 epmd 11.5 Hidden Nodes 11.6 C Nodes 11.7 Security 11.8 Distribution BIFs 11.9 Distribution Command Line Flags 11.10 Distribution Modules 12 Compilation and Code Loading 12.1 Compilation 12.2 Code Loading 12.3 Code Replacement 13 Ports and Port Drivers 13.1 Ports 13.2 Port Drivers 13.3 Port BIFs
a series of articles on what I’ve learned about Erlang (and Elixir) from writing Erl2ex, an Erlang-to-Elixir transpiler. This week we take a look at exception handling: how each language identifies different types of exceptions and how closely they relate. A word about exceptions Erlang and Elixir both provide exception handling facilities: you can throw exceptions when an unexpected condition is detected, and catch them further up the call chain. However, in most cases, this comes with a disclaimer: Don’t handle exceptions. At least, not unless you have an extraordinary situation. Dave Thomas’s book Programming Elixir emphasizes this by relegating the discussion of exceptions to an appendix, with a final subheading of “Now ignore this appendix”. This is for good reason. The much-lauded fault tolerance of the platform is based on separating code from error handling, allowing errors to crash a process and employing supervisors to recover from them. While there are valid use cases for “traditional” exception handling, they are uncommon in Erlang and Elixir applications. That said, a brief look at the exception handling facilities is instructive for understanding the relationship between the two languages. Three exceptions in one try Erlang defines three “classes” of exceptions, corresponding to three different use cases. Exceptions of class error generally denote runtime errors. These include pattern match failures, undefined function calls, and similar cases, including application-defined errors. Exceptions of class exit are raised when a process is killed. Exceptions of class throw may be raised and caught by your application code to perform a non-local return. Elixir also recognizes those same three classes, so exception handling is interchangeable in the two languages. That is, exceptions thrown in either language can be handled in the other. Both languages also share a similar set of language constructs for raising and catching exceptions. Each has a try construct that pattern matches caught errors and provides cleanup capability.