[erlang-questions] About a simple program

全卓 <>
Sun Nov 23 08:12:34 CET 2014


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 !


More information about the erlang-questions mailing list