[erlang-questions] more try/catch confusion

Jayson Vantuyl <>
Wed Apr 7 00:22:29 CEST 2010


There are three types of exceptions in Erlang:

* exits
* errors
* throws

The old "catch" keyword catches all three.  The "try" keyword can differentiate the three.  The catch pattern is usually of the form A:B, where A is one of error, exit, or throw.

When you don't specify the type of exception, it defaults to "throw".  You're getting an exit and the first two default to only catching throw exceptions, hence only the old-style "catch" is really catching the exception.

On Apr 6, 2010, at 3:07 PM, Brian Acton wrote:

> Hi guys,
> 
> I recently went down the road of doing some xml parsing. Using the xmerl
> libraries, I tried writing the following construct:
> 
> 1> try xmerl_scan:file("broken.xml") catch H -> H end.
> 2404- fatal: unexpected_end
> ** exception exit: {fatal,
> 
> {unexpected_end,{file,"broken.xml"},{line,92},{col,4}}}
> 
> I then tried this construct
> 
> 2> try xmerl_scan:file("broken.xml") of I -> I catch J -> J end.
> 2404- fatal: unexpected_end
> ** exception exit: {fatal,
> 
> {unexpected_end,{file,"broken.xml"},{line,92},{col,4}}}
> 
> I finally got this to work:
> 
> 3> case catch xmerl_scan:file("broken.xml") of G -> G end.
> 2404- fatal: unexpected_end
> {'EXIT',{fatal,{unexpected_end,{file,"broken.xml"},
>                               {line,92},
>                               {col,4}}}}
> 
> Now, the question is why do #1 and #2 fail while #3 succeeds? I would have
> expected both #1 and #2 to have caught the exception.
> 
> --b

-- 
Jayson Vantuyl




More information about the erlang-questions mailing list