[erlang-questions] erl_syntax[_lib] error handling

Richard Carlsson carlsson.richard@REDACTED
Sat Apr 19 13:39:26 CEST 2014

On 2014-02-24 21:21 , Tuncer Ayaz wrote:
> erl_syntax and erl_syntax_lib seem to favor throwing exceptions over
> returning error terms. I found this hard to use due to the missing
> documenation of what exceptions can be thrown by each function. Only
> erl_syntax_lib:analyze_application/1 documents one exception it might
> throw, and the documentation for other funs just states the following:
> "An exception is thrown if..."
> The only way to handle erl_syntax[_lib] errors I've seen so far is to
> catch all exceptions, but that can filter/suppress any kind of
> exception.
> What's the recommended way to handle erl_syntax[_lib] errors?

Hi Tuncer. erl_syntax does not try to be systematic about what kind of 
exception gets generated when the input is bad; that would probably make 
the code grow by 50% In general, it will be a function_clause, 
case_clause or badmatch, but there's no good way of separating these 
from an actual bug in erl_syntax. The intended way to use erl_syntax 
with unknown input is to use the type/1 function (throws badarg error if 
it's not a valid AST) and predicates like is_atom/1 to test the input 
before trying to deconstruct it.


More information about the erlang-questions mailing list