如何让 try / catch 在 Erlang 中工作?
我对 Erlang 还很陌生,我正在尝试让基本的 try / catch 语句发挥作用。
我正在使用 webmachine 来处理一些请求,我真正想做的就是解析一些 JSON 数据并返回它。如果 JSON 数据无效,我只想返回错误消息。这是我到目前为止的代码。
请注意,JSON 数据无效。
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).
该代码可以编译,但是当我运行它并发送文本请求时,我收到以下错误。
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}]}}
我到底做错了什么?
文档说“catch”语句可以处理所有错误,或者我是否必须执行某些操作来捕获 mochijson:decode
引发的特定错误。
请任何线索或建议都会有帮助。
I am pretty new to Erlang, and I am trying to get a basic try / catch statement to work.
I am 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.
Note that 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?
The 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
catch 子句
_ -> ...
仅捕获“抛出”类的异常。要捕获其他类型的异常,您需要以Class:Term -> 的形式编写模式。 ...
(即,默认的Class
是throw
)。对于您的情况:当您这样做时,您应该始终问自己为什么要捕获所有可能的异常。如果是因为您正在调用第三方代码而您不知道它的行为方式,那么通常没问题。如果您调用自己的代码,请记住,您基本上会丢弃有关失败的所有信息,这可能会使调试变得更加困难。如果您可以缩小范围以仅捕获特定的预期情况并让任何其他异常落空(这样您就可以看到真正的故障发生在哪里),那么就这样做。
The catch-clause
_ -> ...
only catches exceptions of the 'throw' class. To catch other kinds of exceptions, you need to write a pattern on the formClass:Term -> ...
(i.e., the defaultClass
isthrow
). In your case: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.