[erlang-questions] Line number in errors: badmatch, case clause, badarith
Thomas Lindgren
thomasl_erlang@REDACTED
Tue Jan 12 16:16:59 CET 2010
----- Original Message ----
> From: caio ariede <caio.ariede@REDACTED>
> To: Erlang-Questions Questions <erlang-questions@REDACTED>
> Sent: Tue, January 12, 2010 3:30:00 PM
> Subject: Re: [erlang-questions] Line number in errors: badmatch, case clause, badarith
>
> It works with this type of error too?
>
> > test:foo().
> ** exception error: no function clause matching test:bar(1)
>
> 1 -module(test).
> 2 -compile(export_all).
> 3
> 4 foo() ->
> 5 bar(1).
> 6
> 7 bar(0) -> 0.
>
> There is a way to get what line (5) the error occurred?
Not quite, the parse transform rewrites bar/1 into something like this (the details may differ somewhat):
bar(0) -> 0;
bar(N) -> exit({function_clause, {?MODULE, ?LINE}, [N]}).
So you can see where the error occurred and what the bad value was. The ?LINE used is that of the first clause of bar/1. The same is done for case, if, funs and wherever possible. For builtin functions (including +) you get the equivalent. The transform is not perfect, however. It only operates on Erlang source code, so some places are impossible to instrument.
I guess if you use error/1 instead of exit/1, you will get a backtrace, but not line numbers for the calls in the stack. That would probably need some compiler support and a modification of how error/1 is implemented.
Best,
Thomas
More information about the erlang-questions
mailing list