Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Erlangで例外スタックトレースに引数情報を乗せる方法 ref: http://qiita.com/sile/items/2b11cdd0399e20d34dcc
-module(exception_test).
-export([hoge/1, fuga/1]).
%% @doc erlang:error/1を使って例外送出
hoge(_Arg) ->
error(badarg).
%% @doc erlang:error/2を使って例外送出
fuga(Arg) ->
error(badarg, [Arg]).
$ erl
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit]
Eshell V5.10.4 (abort with ^G)
> c(exception_test).
{ok, exception_test}
%% erlang:error/1の場合のエラー表示およびスタックトレース
> exception_test:hoge(erlang).
** exception error: bad argument
in function exception_test:hoge/1 (exception_test.erl, line 8)
> catch exception_test:hoge(erlang).
{'EXIT',{badarg,[{exception_test,hoge,1,
[{file,"exception_test.erl"},{line,8}]},
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,573}]},
{erl_eval,expr,5,[{file,"erl_eval.erl"},{line,357}]},
{shell,exprs,7,[{file,"shell.erl"},{line,674}]},
{shell,eval_exprs,7,[{file,"shell.erl"},{line,629}]},
{shell,eval_loop,3,[{file,"shell.erl"},{line,614}]}]}}
%% erlang:error/2の場合のエラー表示およびスタックトレース
> exception_test:fuga(erlang).
** exception error: bad argument
in function exception_test:fuga/1
called as exception_test:fuga(erlang) % 引数情報が表示される
> catch exception_test:fuga(erlang).
{'EXIT',{badarg,[{exception_test,fuga,
[erlang], % アリティの代わりに引数が取得できる
[{file,"exception_test.erl"},{line,13}]},
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,573}]},
{erl_eval,expr,5,[{file,"erl_eval.erl"},{line,357}]},
{shell,exprs,7,[{file,"shell.erl"},{line,674}]},
{shell,eval_exprs,7,[{file,"shell.erl"},{line,629}]},
{shell,eval_loop,3,[{file,"shell.erl"},{line,614}]}]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment