Erlang 当其他模式也存在时,如何匹配一个简单的抛出?
我正在学习erlang,我不明白当您同时有Erlang 当其他模式也存在时,如何匹配一个简单的抛出?,erlang,pattern-matching,throw,Erlang,Pattern Matching,Throw,我正在学习erlang,我不明白当您同时有错误和退出时,如何在捕获块中的抛出上进行模式匹配 模块 -module(err). -compile([debug_info]). -export([thro/1]). thro(F)-> try F() of 3->"it worked gt then 3"; 4->"gt then 4"; 5-> throw(44) c
错误和退出时,如何在捕获
块中的抛出
上进行模式匹配
模块
-module(err).
-compile([debug_info]).
-export([thro/1]).
thro(F)->
try F() of
3->"it worked gt then 3";
4->"gt then 4";
5-> throw(44)
catch
error:[Y|[Z|X]]->{Y+Z,2};
exit:[X|Y]->{exit,caught,"exiting with code:"++X};
error:44 -> {"thew on result"} % should it be 44 -> something
end.
用法:
对于第一种情况:err:thro(fun()->error([1,2,3])end.
对于第二种情况:err:thro(fun()->exit([“A”,“b”])end.
现在我想对这个例子:err:thro(fun()->5)end.
抛出是否在错误
模式、退出
模式或无模式中捕获?当还有其他退出/错误模式时,如何处理我的抛出
投掷是以错误模式、退出模式还是无模式捕获
它被捕获在throw
模式中,但是您的throw()
必须位于函数F:
-module(my).
-compile([export_all]).
go(F)->
try F() of
_ -> no_errors
catch
error:[Y|[Z|_]]->{Y+Z,2};
exit:[X|_Y]->{exit,caught,"exiting with code:"++X};
throw:Value -> {throw, Value}
end.
在外壳中:
27> my:go(fun() -> throw(5) end).
{throw,5}
换句话说,try F()
只捕获在F内部发生的错误,而不捕获代码中的其他地方。如果捕获从这里捕获错误:
5-> throw(44)
这样,您就不必编写try F()
,只需编写F()