[Erlang37]error/1 exit/1 exit/2 throw/1的区别

1. error/1

主要是系统用来定义内部错误的: Erlang内建的run time error 一共有10种:

function_clause/case_clause/if_clause/badmatch/badarg/undef/badarith/badfun/badarity/system_limit, 比如:

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

这上面就是触发了error/1,我们也可以手动触发一下。

 2> erlang:error(badarg).
    ** exception error: bad argument        

注意到erlang直接把badarg这种内建的error转成更详细的bad argument,

更进一步,我们也可以使用error/1定义自己的错误

3> erlang:error("this is my own error").
   ** exception error: "this is my own error"

这一次,自定义的错就没有被erlang shell认出来。

2. exit/1 exit/2

exit有internal exits 和 external exits的区别,我们可以使用exit(Pid,Reason)让别一个进程退出。

exit/1和error/1非常相似,很多时候可以通用,便是exit语境是退出,更适合于进程退出的情况,还有一个区别就是

exit/1不会带调用的stack trace信息(方便让其它进程退出时不用带非常大的调用信息,更轻量)。但是error/1会带。

4> catch exit(test).
  {‘EXIT‘,test}
5> 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 它最常用配合 try...of catch 处理嵌套case(可以快速跳出),它所携带的信息最少(比exit/1还少一个‘EXIT‘):

6> catch throw(2+2).
4
7> catch 2+2
4     

上面2个case用的catch,都区分不出结果是throw出来的,还是正常计算得到的结果,所以这也是推荐使用try .. of catch的原因:

8> try throw(2+2) of
8> V -> {ok, V}
8> catch
8>  throw:V -> {error, V}
8> end.
{error,4} 

4. 总结

进程退出使用exit/1或exit/2, 想快速跳出recursion或快速跳回Top-Level函数时用throw/1,尽量不要使用error/1,

如果需要得到调用的stack trace信息,可以自己显式的调用erlang:get_stacktrace().得到当前进程最新一次Exception时的的stacktrace。

    

5. 参考资料:

 5.1. Erlang官方文档:http://erlang.org/doc/reference_manual/errors.html

5.2. Learnyousomeerlang关于Exceptions的介绍: http://learnyousomeerlang.com/errors-and-exceptions

时间: 2024-07-29 23:40:48

[Erlang37]error/1 exit/1 exit/2 throw/1的区别的相关文章

自定义库引用时编译出错“collect2: error: ld returned 1 exit status”

想必很多人都遇到过这样的事情,需要写一个共享库,这个库又不能让所有的人都了解里面是怎么实现的,所以只能提供一个库出来供项目使用,可能是处于保密.核心业务的需要,这些都能够理解.笔者也写了一个简单的东西测试了一下,里面还是有不少东西. "collect2: error: ld returned 1 exit status"首先就遇到了这个问题.查了一下,这种错误网上说法也一大堆,起作用的还不多.在android系统里面加的共享库,需要加"namespace android &q

gcc/g++ 编译时出现:“对’xxxx’未定义的引用,collect2: error: ld returned 1 exit status” 的错误

出现的问题: 在使用 make 编译实现一个程序时,出现了下面的错误.查看程序源文件所在的目录时发现程序已经完成了编译,并生成了 list_repo.o 的文件,说明是在程序链接生成可执行文件时发生了问题. storages/local.o:在函数‘LocalStorage::init(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,

error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status

Windows服务器Azure云编译安装MariaDB教程 www.111cn.net 编辑:future 来源:转载 安装MariaDB数据库最多用于linux系统中了,下文给各位介绍在Windows服务器Azure云编译安装MariaDB教程,希望本文能帮助到各位. 试用1元Windows Azure,带宽都是杠杠的.下面演示下Windows Azure下编译安装LNMP环境,系统环境是Ubuntu 14.04 TLS版 软件版本: 数据库:mariadb-10.0.13 Stable PH

php7 编译 collect2: error: ld returned 1 exit status

/usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol 'ber_strdup'/usr/bin/ld:note: 'ber_strdup' is defined in DSO /lib64/liblber-2.4.so.2 so try adding it to the linker command line/lib64/liblber-2.4.so.2:could not read symbols: Invalid

error: ld returned 1 exit status

[email protected]5437:~/Downloads/LBD_Descriptor/build$ cmake .. -- Configuring done -- Generating done -- Build files have been written to: /home/wj/Downloads/LBD_Descriptor/build [email protected]-Inspiron-5437:~/Downloads/LBD_Descriptor/build$ mak

error: ld returned 1 exit status 和 error:undefined reference

undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit status 链接文件没有建立 案例 1 #include<vector> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstdio> 5 using namespace std; 6 class Sol

svn Error: post-commit hook failed (exit code 127) with output

Command: Commit Modified: C:\Users\xsdff\Desktop\project\index.html Sending content: C:\Users\xsdff\Desktop\project\index.html Committing transaction...: Completed: At revision: 5 Error: post-commit hook failed (exit code 127) with output: Error: /sv

QT编译出现ld.exe: cannot open output file debug\nh_03testCallPy.exe: Permission denied collect2.exe: error: ld returned 1 exit status

D:/Qt/Qt5.9.0/Tools/mingw530_32/bin/../lib/gcc/i686-w64-mingw32/5.3.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot open output file debug\nh_03testCallPy.exe: Permission denied collect2.exe: error: ld returned 1 exit status 原因目前可能有如下: 1.程序已经在运行,进入

git did not exit cleanly (exit code 1) 的解决办法

问题描述: 关于Git的使用,在通常情况下,习惯于先在本地创建一个本地仓库,然后将项目提交到本地master,再将本地master中的项目Push 到远程仓库中,这样问题就来了. 具体错误信息如下: git.exe push --progress "origin" master To https://github.com/Jackson-AndyLau/CMSManager.git! [rejected]        master -> master (non-fast-for