4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Erlangで例外スタックトレースに引数情報を乗せる方法

4
Posted at

erlang:error/1を使って例外(ex. badarg)を送出すると、補足した後のerlang:get_stacktrace/1の結果に例外が発生した関数のアリティ情報(引数数)しか載っていなくて苦労した覚えがあるが、erlang:error/2を使えば、引数の完全な情報を載せることができる模様。

exception_test.erl
-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]).
shell
$ 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}]}]}}
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?