[color=blue]触发异常[/color]
[color=blue]try...catch[/color]
[color=blue]try...of...catch[/color]
[color=blue]try...after[/color] 不管try中执行情况如何,after都会执行,这个机制通常用于资源释放
throw(Term)
exit(Reason)
erlang:error(Reason)
特列:exit(normal),进程调用它所跑出的异常不会被捕获,该进程正常终止
[color=blue]try...catch[/color]
try
unsafe_fun()
catch
ErrType:Reason ->
do_something()
end.
[color=blue]try...of...catch[/color]
try
unsafe_fun()
of
O ->
io:format("do nothing");
N ->
do_something_with(N)
catch
_:_ ->
do_something()
end
of...之间的内容,不在try的保护范围
[color=blue]try...after[/color] 不管try中执行情况如何,after都会执行,这个机制通常用于资源释放
{ok, Fd} = file:open("test.txt", [read]),
try
do_something_with_file(Fd)
after
file:close(Fd)
end