[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. 扩展(gen_server中的几种退出进程方法比较)
5.1. 在init不成功时返回{stop, Reason} 退出;
5.2. 处理消息出错,或正常退出时返回{stop, Reason, NewState});
5.3. 使用exit直接退出进程;
5.4. 使用throw退出进程;
5.5. 使用error退出进程(由于我们前面讲了error主要用于定义内部错误,所以不推荐使用)。
try_dispatch(Mod, Func, Msg, State) ->
try
{ok, Mod:Func(Msg, State)}
catch
throw:R ->
{ok, R};
error:R ->
Stacktrace = erlang:get_stacktrace(),
{'EXIT', {R, Stacktrace}, {R, Stacktrace}};
exit:R ->
Stacktrace = erlang:get_stacktrace(),
{'EXIT', R, {R, Stacktrace}}
end.
使用stop就是正常退出,不带stack trace信息(他本来就没有crash,也没有stack信息)
使用exit退出,它带了stack trace信息。
{noreply, NewState}
{reply, ok, NewState}
{stop, normal, NewState}
Notice that for any other reason than normal, shutdown, or {shutdown,Term}, the gen_server process is assumed to terminate because of an error and an error report is issued using error_logger:format/2.
=ERROR REPORT==== 31-Oct-2016::15:10:44 ===
** Generic server test_throw_exit terminating
** Last message in was go
** When Server state == {state}
** Reason for termination ==
** not_shutdown_term
所以,如果我们在正常退出或shutdown时不需要那一列烦人的log,就把reason定义为三者中的一种就行了。
具体源码可见: https://github.com/erlang/otp/blob/maint/lib/stdlib/src/gen_server.erl#L809
6. 参考资料:
Erlang官方文档:http://erlang.org/doc/reference_manual/errors.html
Learnyousomeerlang关于Exceptions的介绍: http://learnyousomeerlang.com/errors-and-exceptions

[Erlang37]error/1 exit/1 exit/2 throw/1的区别的更多相关文章
- error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status
Windows服务器Azure云编译安装MariaDB教程 www.111cn.net 编辑:future 来源:转载 安装MariaDB数据库最多用于linux系统中了,下文给各位介绍在Window ...
- 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\ ...
- error: ld returned 1 exit status 和 error:undefined reference
undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit sta ...
- docker pull报错failed to register layer: Error processing tar file(exit status 1): open permission denied
近来在一个云主机上操作docker pull,报错如下: failed to register layer: Error processing ): open /etc/init.d/hwclock. ...
- Arduino上“Collect2.exe: error: ld returned 5 exit status”错误的解决方法
1.运行环境 Windows xp; Arduino1.6.11 IDE. 2.问题 在Arduino编译时,经常出现如下的错误: collect2.exe: error: ld returned 5 ...
- docker mac 命令行登录报错处理 : Error saving credentials: error storing credentials - err: exit status 1
参考:https://blog.csdn.net/xufwind/article/details/88756557 比较新版本的docker命令行登录会出现以下错误: Error saving cre ...
- moc_XXXX.o:(.data.rel.ro._ZTI12CalculatorUI[_ZTI12CalculatorUI]+0x10): undefined reference to `typeinfo for QWidget' collect2: error: ld returned 1 exit status make: *** [Makefile:144: myCalculator]
main.cpp:(.text.startup+0x22): undefined reference to `QApplication::QApplication(int&, char**, ...
- DevC++ 报错[Error] Id returned 1 exit status
DevC++ 报错[Error] Id returned 1 exit status 起因 学校机房的计算机总是二次编译总是报错 报错提示 [Error] Id returned 1 exit sta ...
- Dev C++编写C/C++程序 出现[Error] ld returned 1 exit status报错分析及解决
debug系列第一弹,不知道大家写程序的时候是不是都遇到过如题的报错. 我本人是经常遇到这行熟悉的令人不知所措的报错,可能是我太笨了 有时候百度无果也差不到原因,那就汇总一下目前我遇到的情况吧--持续 ...
随机推荐
- 【读书笔记】iOS-Xcode知识-多线程
一,Xcode使用的调试器是GDB.GDB是GNU项目的一部分,它可以在很多不同的平台上使用.如果你愿意,可以通过命令行来运行它.GDB有着完善的文档系统,尽管它的文档有些难于理解并且网络上流传着好几 ...
- 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(四)Alert View视图 学习笔记
当我们的应用电量不足的时候,就需要警告提示,那么我们可以用Alert View视图 实现:
- Swift面向对象基础(上)——Swift中的类和结构体(下)
学习来自<极客学院> import Foundation class User { var name:String var age:Int init(name:String,age:Int ...
- (传输层)UDP协议
目录 数据单位特点具体实现要求UDP首部格式发送UDP请求的客户端图释 数据单位 UDP 传送的数据单位协议是 UDP 报文或用户数据报 特点 UDP 是无连接的,即发送数据之前不需要建立连接 UDP ...
- Dev Grid拖拽移动行
效果图 源码下载 拖拽时带行截图效果实现代码 /// <summary> /// 拖拽帮助类 /// </summary> public static class DragHe ...
- Effective Java 25 Prefer lists to arrays
Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...
- 大数据架构-使用HBase和Solr将存储与索引放在不同的机器上
大数据架构-使用HBase和Solr将存储与索引放在不同的机器上 摘要:HBase可以通过协处理器Coprocessor的方式向Solr发出请求,Solr对于接收到的数据可以做相关的同步:增.删.改索 ...
- libc.so.6被删后导致系统无法使用的原因及解决方法
记一次升级glibc库发生的错误 今天给glibc库升级,发生了一件让我吓出一声汗的事情,我把动态库中的libc.so.6给删了,瞬间所有的非系统命令都无法使用,使用就报错 当时就吓尿了,生产环境被我 ...
- PHP通过串口发短信
随技术进步,短信收发领域按时间先后产生了三种模式:BLOCK MODE,基于AT指令的TEXT MODE,基于AT指令的PDU MODE.其中,TEXT MODE比较简单,多款诺基亚手机均支持此款模式 ...
- Hadoop Yarn core concepts
The fundamental idea of YARN is to split the two major responsibilities of the JobTracker—that is, r ...