[erlang-questions] dialyzer <-> exceptions

Kostis Sagonas kostis@REDACTED
Mon May 5 16:59:41 CEST 2008


Andras Georgy Bekes wrote:
> 
> I've just found that dialyzer is not type checking exceptions.
> 
> What I mean:
> -----------------------------------------
> f() ->
>   ok.
> 
> case catch f() of
>    ok ->
>       ok;
>    _ ->
>       nok
> end,
> -----------------------------------------
> Here dialyzer approximates the type of catch f() to 'any' type, and 
> therefore not complaining about the second, unreachable case.
> 
> With try-catch, things are better:
> -----------------------------------------
> try f() of
>    ok ->
>       ok;
>    _ ->   %% detected as unreachable
>       nok
> catch
>    _:_ -> %% not detected as unrechable
>       something
> end,
> -----------------------------------------
> 
> So it approximates caught or try-caught exception values with 'any' type 
> even if no exception can occur.

This thread is very much related to the thread we had last week about 
catch vs. try-catch, the latter being a much much nicer construct IMO.

Dialyzer currently bases its analysis on something called success 
typings, which approximates successful *returns* from functions, not 
exceptions that a function might possibly throw.  There is a rather 
fundamental reason for this: in Erlang in principle any function can 
throw some exception.

In the case of catch f() to detect that the second branch is unreachable 
dialyzer would have to find that f() cannot possibly throw some 
exception. (This is obviously easy in this case, but complicated in 
general.)

In the case of try-catch, its clear that _ refers to return values only, 
not exceptions.  One more (very good, IMO) reason to prefer try-catch.

> My questions are: Will dialyzer type-check exceptions in the future, or 
> this would imply a total rewrite of it and therefore not planned? If 
> yes, when?

The answer to your question is: tracking exceptions requires a 
significantly different kind of analysis than the one currently present 
in dialyzer to infer the exception information, but it does not require 
a total rewrite of the pass which generates the warnings.  It's not 
something that we have currently planned for, but it is something we 
have in our minds.  Whether it will actually happen or not depends on 
mundane issues such as funding either from a research foundation or from 
some other interested source.

Kostis



More information about the erlang-questions mailing list