《Erlang程序设计》学习笔记-第4章 异常
第4章 异常
摘自:http://hi.baidu.com/zai215837829/blog/item/86a1953e72b230f655e7231d.html
1. 抛出异常的情况:(1)系统内部错误;(2)throw(Exception);(3)exit(Exception);(4)erlang:error(Exception)
2. exit(Why) 当当前进程想退出时要调用这个函数,它会产生异常。如果这个异常没有被捕获,那么系统会向所有与当前进程相连接的进程广播{'EXIT',Pid,Why}消息。
3. throw(Why) 抛出一个调用者会捕获的异常
4. erlang:error(Why) 抛出一个“崩溃错误”,这个错误调用者不会真正意识到要去处理,相当于系统内部的错误。
5. try...catch语法:
try FuncOrExpressionSequence of Pattern1 [when Guard1] -> Expressions1; Pattern2 [when Guard2] -> Expressions2; ... %% 最后一个不能加分号 catch ExceptionType: ExPattern1 [when ExGuard1] -> ExExpressins1; ExceptionType: ExPattern2 [when ExGuard2] -> ExExpressins2; ... %% 最后一个不能加分号 after AfterExpressions %% 这个不能加分号 end
try F catch ... end
try F of Val -> Val catch ... end
case f(X) of {ok, Val} -> do_something_with(Val); {error, Why} -> %% do something end,
try f(X) catch throw:{thisErro, X} -> ... throw:{otherErro, X} -> ... end
case (catch foo(...)) of {'EXIT', Why} -> ... Val -> ... end