Should I use try catch in Erlang or do I need to pass an error token?

I am coding in Erlang, and I'm a little unsure how to handle error handling, especially after looking at the ugly errors that Erlang always returns.

Should I use try catch in Erlang or do I need to pass an error token?

+3
source share
4 answers

There are three main ways to handle exceptions in serial Erlang:

  • throws ( throw(Term))
  • errors ( erlang:error(Reason))
  • outputs ( exit(Reason))

Throws , (, , ). , , . , stdlib, - {error, Reason}, try...catch, . , .

, , . , . , if case ... of , , , , .. ( , ).

, , . , , , , - .

( , , !), , . .

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


- . - (/ ), , : , , , , .

: exit(Pid, Reason). "exit" . process_flag(trap_exit, true) , receive.

, , exit(Pid, kill), - . , Pid, {'EXIT', killed}.

- , , . , , ; , .

+6

, , "".

, , " ".

+3

, , . API : , , , , .

API file:consult/1, , Erlang. with-open-file, lisp, , .

, , , , .

You want to let the programmer write readable code for a successful case . If you expect the file to not exist, a simple example of the return value will be prettier than exception handling. If your successful case is to assume that the file exists and just read it, then the exception will certainly fail as soon as it does not delay.

Choosing any particular approach because you think Erlang has ugly error messages doesn't seem like a good guide.

+3
source

Source: https://habr.com/ru/post/1733014/


All Articles