Erlang(7):順序程序的錯誤處理

顯式生成錯誤的方法:
exit(Why) 廣播一個信號給當前進程連接的全部進程。
throw(Why) 拋出一個調用者可能想要捕捉的異常錯誤。
error(Why) 指示奔潰性錯誤,很是嚴重的錯誤。


捕捉錯誤,try...catchthis

try FuncOrExpressionSeq of
	Pattern1[when Guard1] -> Expressions1;
	Pattern2[when Guard2] -> Expressions2;
	...
catch
	ExceptionType1: ExPattern1 [when ExGuard1] -> ExExpressions1;
	ExExpressions2: ExPattern2 [when ExGuard2] -> ExExpressions2;
	...
after
	AfterExpressions
end

提供了歸納的信息code


catch 語句,比try...catch更加早引入Erlang。遊戲

提供了詳細的棧跟蹤信息。進程


常常出現錯誤的時候的代碼:開發

case f(x) of
    {ok, Val} ->
         do_some_thing_with(Val);
    {error, Why} ->
          %% ... 處理這個錯誤...
end,
...


錯誤可能有但罕見時的代碼

try my_func(X)
catch
    throw:{thisError, X} -> ...
    throw:{someOtherError, X} -> ...
end


捕捉一切異常錯誤:

try Expr
catch
    _:_ -> ... 處理全部異常錯誤的代碼
end

erlang:get_stacktrace()得到最近的棧跟蹤信息。


有錯誤,就要儘可能往外面拋,展示出來。讓程序完全奔潰。這是Erlang 的原則。get

或許吧。it

可是不是作遊戲的原則。io

至少不是作遊戲服務端的原則。class

咱們能夠容忍錯誤的存在。由於咱們是快速開發。bug是修不完的。erlang

只能建一個修一個。

相關文章
相關標籤/搜索