[erlang-questions] About a simple program

Vlad Dumitrescu <>
Sun Nov 23 09:35:52 CET 2014


Hi!

What would you expect the code would do?

Try replacing lists:foreach with lists:map and notice that the result is
[a, throw_exception, error_exception, exit_exception, {'EXIT',a}]. Maybe
this is what you wanted?

best regards,
Vlad


On Sun, Nov 23, 2014 at 8:12 AM, 全卓 <> wrote:

> Hi,
>     I write a simple Erlang program,But it doesnot function like what i
> think.
>     Here is my source code.
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> -module(exception).
> -export([start/0]).
>
> generate_exception(1) ->
>     a;
> generate_exception(2) ->
>     throw(throw_exception);
> generate_exception(3) ->
>     error(error_exception);
> generate_exception(4) ->
>     exit(exit_exception);
> generate_exception(5) ->
>     {'EXIT',a}.
>
> start() ->
>     lists:foreach(fun (I) -> try generate_exception(I) of
>                  Res -> Res
>                  catch
>                  throw:T -> T;
>                  exit:E -> E;
>                  error: Er -> Er
>                  after
>                  io:format("exec this line:~p~n",[I])
>                  end
>           end,lists:seq(1,5)).
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> I compile it and run it ,the result is:
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> exec this line:1
> exec this line:2
> exec this line:3
> exec this line:4
> exec this line:5
> ok
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> It seems that no exception has been caught, why?Or,I have something wrong?
> By the way,My Otp version is: Erlang/OTP 17,Install from source code,
> OS is Ubuntu 14.04.
>
> Thank you !
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141123/81dc7719/attachment.html>


More information about the erlang-questions mailing list