[erlang-questions] About a simple program

Lhfcws GMail <>
Mon Nov 24 04:51:02 CET 2014


Because you did not output the exit or error, and after block is just like finally block in Java, so it’ll always be executed. Just try modify catch block as follow:
throw:T -> io:write(T), io:format("~n");
exit:E -> io:write(E), io:format("~n");
error:Er -> io:write(Er), io:format("~n")

在 2014年11月23日,15:12,全卓 <> 写道:

> 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/20141124/5bdb445d/attachment.html>


More information about the erlang-questions mailing list