我应该在Erlang中使用try catch,还是应该传回一个错误标记? [英] Should I use try catch in Erlang or should I pass back an error token?

查看:168
本文介绍了我应该在Erlang中使用try catch,还是应该传回一个错误标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Erlang中编码,我不确定如何处理错误处理,特别是在看到Erlang总是返回的丑恶错误之后。



我应该使用尝试抓住Erlang或者我应该传回一个错误标记?

解决方案

在顺序Erlang中有三种基本的方法来执行异常处理:


  1. throws( throw(Term)

  2. 错误( erlang:error(Reason)

  3. exit( exit(Reason) / code>)

投掷用于非本地退货和某些种类例外,您希望能够处理(可能是因为它们经常发生)。当它被提出时,您应该尝试停止它,然后才能到达模块之外。在stdlib中使用的通常方法是抛出一个元素,它将被<$ c中的顶级函数所捕获, {error,Reason} $ c> try ... catch ,然后将元组返回给用户。然后,用户可以根据该返回值来决定要执行的操作。另一方面,



指向不可恢复的异常。他们通常要求用户更改他的代码。它们包括运行时错误,如 if case ... of 不能匹配的分支,可以 t匹配或不存在等。此错误的目的是崩溃,并且在大多数情况下不应在本地捕获或处理该过程(具有主管或监视进程选择错误消息,然后将其记录为您或在界面级别处理用户)。



退出是在您特意要求进程终止时使用的。有时使用退出或错误有时不清楚,但是我给出的提示是区分意图。



错误和退出很少应该被捕获和处理顺序地(你真的要确定你知道如何解决事情!),因为其他的过程是适当的来处理它。让它崩溃。



(更多详情: http: //learnyousomeerlang.com/errors-and-exceptions






下一个级别是处理错误在多进程环境中。在这个时候做的事情的标准方法是将流程(和/或使用主管)连接起来,以获取死机进程及其原因:重新启动它们,记录消息,进行维护,升级,同时系统保持滚动



您可以为多个进程获取新的异常函数: exit(Pid,Reason)。这可以让您在另一个进程上调用退出。在这种情况下,必须通过在监视器进程中设置 process_flag(trap_exit,true)来完成错误处理,然后可以通过标准的接收表达式。



请注意,一种特殊的退出,即 exit(Pid,kill) code>将终止一个进程,没有任何可能抓住它。与 Pid 相关联的其他进程应该会收到 {'EXIT',被杀死} 形式的信号。 >

使用监督树是如何确保您的程序继续运行。为了确保你不会腐败任何东西,早点崩溃也是至关重要的早期有问题的代码停止运行,更容易将其全部清理。


I'm coding in Erlang and a I am bit unsure about how to approach error handling, especially after seeing the ugly errors Erlang always returns.

Should I use try catch in Erlang or should I pass back an error token?

解决方案

There are three basic ways to do exception handling in sequential Erlang:

  1. throws (throw(Term))
  2. errors (erlang:error(Reason))
  3. exits (exit(Reason))

Throws are to be used for non-local returns and some kinds of exception you expect to be able to handle (maybe because they occur often). When that one is raised, you should try and stop it before it gets outside your module. The usual way it is used in the stdlib is to throw a tuple of the kind {error, Reason} which will be caught by a top-level function in a try...catch before returning the tuple to the user. The user can then decide what to do based on that return value.

Errors, on the other hand, point to non-recoverable exceptions. They're usually going to require the user to change his code. They include runtime errors like if or case ... of branches that can't be matched, functions that can't match or don't exist, etc. The purpose of this error is to crash, and should not be caught or handled locally to the process in most cases (have a supervisor or monitoring process pick up the error message and then log it for you or handle it for the user at the interface level).

Exits are to be used when you specifically want the process to terminate. It's sometimes a bit unclear when to use exits or errors, but the tip I've been given is to differentiate the intent.

Errors and exits seldom should be caught and handled sequentially (you've really got to be sure you know how to fix things!), as other process are in place to deal with it. Let it crash.

(more details: http://learnyousomeerlang.com/errors-and-exceptions)


The next level is when dealing with errors in a multi-process environment. The standard way to do things at that point is to link processes together (and/or use supervisors) to pick up dead processes and the reason why they did: restart them, log the message, do your maintenance, upgrade while the system keeps rolling.

You get a new exception function for multiple processes: exit(Pid, Reason). This lets you call 'exit' on another process. In this case, error handling has to be done by setting process_flag(trap_exit, true) in the monitor process, after which you can get exit signals through a standard receive expression.

Note that a special kind of exit, namely exit(Pid, kill) will terminate a process without any possibility to catch it. Other processes linked to Pid should then receive a signal of the form {'EXIT', killed}.

Using supervision trees is how you make sure your programs keep running. Crashing early is also essential in order to make sure you won't corrupt anything; the earlier problematic code stops running, the easier it is to clean it all up.

这篇关于我应该在Erlang中使用try catch,还是应该传回一个错误标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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