Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Erlang 当其他模式也存在时,如何匹配一个简单的抛出?_Erlang_Pattern Matching_Throw - Fatal编程技术网

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

我正在学习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)
    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()