如何捕获Erlang中的异常消息? [英] How to catch Exception Message in Erlang?

查看:163
本文介绍了如何捕获Erlang中的异常消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Gen_server未启动时抛出的异常消息。

This is the Exception message thrown by Gen_server when its not started.

(ankit@127.0.0.1)32> R11 = system_warning:self_test("SysWarn").
** exception exit: {noproc,
                    {gen_server,call,
                     [system_warning_sup,
                      {start_child,
                       {system_warning_SysWarn,
                        {system_warning,start_link,[{system_warning_SysWarn}]},
                        permanent,10,worker,
                        [system_warning]}},
                      infinity]}}
     in function  gen_server:call/3
     in call from system_warning_sup:'-start_child/1-lc$^0/1-0-'/1
     in call from system_warning:self_test/1
(ankit@127.0.0.1)33> R11.
* 1: variable 'R11' is unbound

现在,我想做的是抓住这个异常消息&放入变量R11(以上显示为未绑定)。我想这样做,因为如果gen_sever没有启动,那么我想要得到这个消息后开始。我也尝试使用handle_info但不能捕获异常或可能无法正确实现。任何身体都可以帮助我解决这个问题,例如提供一些代码。

Now, What I want to do is to catch this exception message & put into variable R11 (showed above as unbound). I want to do so because if gen_sever is not started then I want to start after getting this message. I also tried using handle_info but not able to trap the exception or may be not able to implement it correctly. Can any body please help me with this problem providing some code for example.

推荐答案

来自@ W55tKQbuRu28Q4xv和@Zed的答案都是正确的,但有点简洁。 : - )

Both the answers from @W55tKQbuRu28Q4xv and @Zed are correct but a little terse. :-)

有两种方法可以在本地捕获错误: catch try 。两者也将捕获由 throw 生成的非本地回报。

There are two ways to locally catch an error: catchand try. Both will also catch non-local returns generated by throw.

catch 是旧的和更简单的两个,并具有语法 catch Expr 。如果正在评估的表达式中出现错误,那么 catch 返回 {'EXIT',ErrorValue} ,否则只返回表达式的值。一个问题是,没有办法看到错误返回值是如何生成的,因此可以很容易地在表达式中伪造。以同样的方式,您无法看到返回值是否来自 throw 。注:这是不是一个错误,但是一个功能。另外它是一个低优先级的前缀运算符,所以通常使用它,如:

catch is the older and simpler of the two and has the syntax catch Expr. If an error occurs in the expression being evaluated then catch returns {'EXIT',ErrorValue}, otherwise it just returns the value of the expression. One problem with it is that there is no way to see how the error return value has been generated so it can easily be faked in the expression. In the same way you can't see if the return value comes from a throw. N.B. this is not a bug but a feature. Also it is a prefix operator with a low priority so you would normally use it like:

R11 = (catch system_warning:self_test (....))

以避免混淆。这是一个错误,它应该是 catch ... end

to avoid confusion. This was a mistake, it should have been catch ... end.

throw 更复杂,可以更好地控制要捕获的内容,以及如何处理正常返回和错误/非本地返回。有关完整的说明,请参阅手册。 @ Zed的例子显示了捕捉一切的最简单的例子。

throw is more complex and allows you much greater control over over what to catch and how to handle both the normal returns and error/non-local returns. See the manual for a full description. @Zed's example shows the simplest case which catches everything.

这篇关于如何捕获Erlang中的异常消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆