如何获得尝试/抓住在erlang工作 [英] How to get try / catch to work in erlang

查看:144
本文介绍了如何获得尝试/抓住在erlang工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢erlang,而且我试图让一个基本的try / catch语句工作。我使用webmachine来处理一些请求,所有我真正想做的是解析一些JSON数据并返回它,如果JSON数据无效,我只想返回一个错误msg,这里是我有的代码到目前为止。



(JSON数据无效)

  to_text (ReqData,Context) - > 
Body ={\firstName\:\John\\lastName\:\Smith\},
尝试解码(正文)
_ - > {成功! Json decoder!,ReqData,Context}
catch
_ - > {Error! Json无效,ReqData,Context}
end。


decode(Body) - >
{struct,MJ} = mochijson:decode(Body)

代码编译,但是当我运行它并发送文本请求时,我得到以下错误返回。

 错误,{error,{case_clause,{{const,lastName},
:\Smith\},
{decoder,utf8,null,1,31,逗号}}},
[{mochijson,decode_object,3},
{ mochijson,json_decode,2},
{webmachine_demo_resource,test,1},
{webmachine_demo_resource,to_text,2},
{webmachine_demo_resource,to_html,2},
{webmachine_resource, resource_call,3},
{webmachine_resource,do,3},
{webmachine_decision_core,resource_call,1}]}}

我究竟在做错什么? ecatch语句处理所有错误,或者我必须做一些事情来捕获由mochijson:decode抛出的特定错误。
请任何潜在客户或建议将有所帮助。谢谢。

解决方案

catch子句_ - > ...只捕获'throw'类的异常。要捕获其他种类的异常,您需要在Class:Term - > ...表单上写入一个模式(即默认的Class为'throw')。在你的情况下:

  catch 
_:_ - > {Error!Json is invalid,ReqData,Context}
end

当你做这个,你应该永远问自己为什么你正在抓住每一个可能的例外。如果是因为您打电话给第三方代码,您不知道它可能的行为,通常可以。如果您打电话给自己的代码,请记住,您基本上是丢弃所有关于故障的信息,从而使调试更加困难。如果您可以缩小范围,仅捕获特定的预期情况,并允许其他任何异常通过(所以您看到真正的失败发生在哪里),那么这样做。


i'm pretty new to erlang and i'm trying to get a basic try / catch statement to work. I"m using webmachine to process some requests and all i really want to do is parse some JSON data and return it. In the event that the JSON data is invalid, I just want to return an error msg. Here is the code I have so far.

(the JSON data is invalid)

to_text(ReqData, Context) ->    
    Body =  "{\"firstName\": \"John\"\"lastName\": \"Smith\"}",
    try decode(Body) of
  _ -> {"Success! Json decoded!",ReqData,Context}
 catch
  _ -> {"Error! Json is invalid",ReqData,Context}
 end.         


decode(Body) ->
  {struct, MJ} = mochijson:decode(Body).

The code compiles, but when i run it, and send a request for the text, i get the following error back.

error,{error,{case_clause,{{const,"lastName"},
                            ": \"Smith\"}",
                            {decoder,utf8,null,1,31,comma}}},
              [{mochijson,decode_object,3},
               {mochijson,json_decode,2},
               {webmachine_demo_resource,test,1},
               {webmachine_demo_resource,to_text,2},
               {webmachine_demo_resource,to_html,2},
               {webmachine_resource,resource_call,3},
               {webmachine_resource,do,3},
               {webmachine_decision_core,resource_call,1}]}}

What exactly am i doing wrong? documentation says the "catch" statement handles all errors, or do i have to do something to catch a specific error that is thrown by mochijson:decode. Please any leads or advice would be helpful. Thanks.

解决方案

The catch-clause "_ -> ..." only catches exceptions of the 'throw' class. To catch other kinds of exceptions, you need to write a pattern on the form "Class:Term -> ..." (i.e., the default Class is 'throw'). In your case:

catch
  _:_ -> {"Error! Json is invalid",ReqData,Context}
end

When you do this, you should always ask yourself why you're catching every possible exception. If it's because you're calling third-party code that you don't know how it might behave, it's usually OK. If you're calling your own code, remember that you're basically throwing away all information about the failure, possibly making debugging a lot more difficult. If you can narrow it down to catching only particular expected cases and let any other exceptions fall through (so you see where the real failure occurred), then do so.

这篇关于如何获得尝试/抓住在erlang工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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