[ERLANG37]ERROR/1 exit/1 EXIT/2 THROW/1 the difference

Source: Internet
Author: User
Tags stack trace

1. ERROR/1

The system is primarily used to define internal errors: There are 10 types of run time error built into Erlang:

Function_clause/case_clause/if_clause/badmatch/badarg/undef/badarith/badfun/badarity/system_limit, For example:          

1> erlang:binary_to_list (1).     * * exception Error:bad argument in       function  binary_to_list/1       called as Binary_to_list (1)

This is triggered by the ERROR/1, we can also manually trigger a bit.

2> Erlang:error (badarg).         * * Exception Error:bad  argument

Note that Erlang directly Badarg this built-in error into a more detailed bad argument,

Further, we can also use ERROR/1 to define our own errors.

3> Erlang:error ("This is my own error").    * * Exception Error: "This is my own error"

This time, the custom error is not recognized by the Erlang shell.

2. EXIT/1 EXIT/2

Exit has the difference between internal exits and external exits, we can use Exit (Pid,reason) to let a process exit.

EXIT/1 and ERROR/1 are very similar, many times can be common, that is, exit context is a withdrawal, more suitable for the process of exit situations, there is a difference is

EXIT/1 does not bring the stack trace information that is called (it is easier for other processes to exit without having to take very large call information, lighter). But ERROR/1 will take.

Catch exit (test).  {' EXIT ', test}catch  error (test).   {' EXIT ', {test,[{erl_eval,do_apply,6,           [{file,' Erl_eval.erl '},{line,674}]},            {Erl_ eval,expr,5,[{file, "Erl_eval.erl"},{line,431}]},            {Shell,exprs,7,[{file, "Shell.erl"},{ line,686}]},            {Shell,eval_exprs,7,[{file, "Shell.erl"},{line,641}]},            {shell,eval_loop, 3,[{file, "Shell.erl"},{line,626}]}]}}

3. THROW/1

THROW/1 It is most commonly used with try...of catch processing nested case (which can jump out quickly) and it carries the least amount of information (a ' EXIT ' less than EXIT/1):

catch throw (+/-). catch 2+24 

The above 2 case catch, can not tell whether the result is a throw out, or normal calculation results, so it is recommended to use try. Reasons for the catch:

Try of 8> V, {OK, v}catch8>  throw:v , {error, v}end . {error,4} 

4. Summary :

Process exits using EXIT/1 or EXIT/2, want to quickly jump out of recursion or jump back to top-level function with THROW/1, try not to use ERROR/1,

If you need to get the stack trace information for the call, you can explicitly call erlang:get_stacktrace () yourself. Gets the stacktrace of the current process when the latest exception.

5. References:

5.1. Erlang Official document:http://erlang.org/doc/reference_manual/errors.html

5.2. Learnyousomeerlang's introduction to Exceptions: http://learnyousomeerlang.com/errors-and-exceptions

[ERLANG37]ERROR/1 exit/1 EXIT/2 THROW/1 the difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.