17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

58<br />

The stack trace is a list, <strong>in</strong> reverse order (last call first), of the calls nearest the top of the<br />

stack when the exception occurred. Each function is represented as {Module, Function,<br />

Args}, where Module <strong>and</strong> Function are atoms, <strong>and</strong> Args is either the arity of the function, or<br />

the actual list of arguments to the call, depend<strong>in</strong>g on what <strong>in</strong>formation was available at the<br />

time. Typically, only the topmost call might have an argument list.<br />

Note that if you call erlang:get_stacktrace() <strong>and</strong> get an empty list, it simply means that<br />

no exception has been caught by this process yet.<br />

2.8.6 – Re-throw<strong>in</strong>g<br />

It might happen that you need to exam<strong>in</strong>e an exception more closely before you decide<br />

whether to catch it or not. Although this is unusual, you can then catch it first <strong>and</strong> re-throw it<br />

if necessary, us<strong>in</strong>g the built-<strong>in</strong> function erlang:raise(Class, Reason, Stacktrace). Here, Class<br />

must be one of error, exit, or throw, <strong>and</strong> Stacktrace should be what you got from<br />

erlang:get_stacktrace(). For example:<br />

try<br />

do_someth<strong>in</strong>g(…)<br />

catch<br />

Class:Reason -><br />

Trace = erlang:get_stacktrace(),<br />

case analyze_exc(Class, Reason) of<br />

true -> h<strong>and</strong>le_exc(Class, Reason, Trace),<br />

false -> erlang:raise(Class, Reason, Trace)<br />

end<br />

end<br />

In the above, we catch any exception, analyze it, <strong>and</strong> either h<strong>and</strong>le it ourselves or rethrow<br />

it. It is however both messy <strong>and</strong> <strong>in</strong>efficient (s<strong>in</strong>ce it requires creat<strong>in</strong>g the symbolic<br />

stack trace as a list of tuples) <strong>and</strong> should only be done if you see no better solution.<br />

2.8.7 – Pla<strong>in</strong> old catch<br />

Before try–expressions were added to the language, there was just catch. You’ll see a lot<br />

of this <strong>in</strong> older code, s<strong>in</strong>ce it was the only way of h<strong>and</strong>l<strong>in</strong>g exceptions. It works like this:<br />

“catch Expression” will evaluate Expression (which can be any expression), <strong>and</strong> if it<br />

produces a result (i.e., doesn’t throw an exception), you’ll simply get that result. Otherwise,<br />

if there is an exception, it will be caught <strong>and</strong> presented as the result of the catch, us<strong>in</strong>g<br />

different conventions depend<strong>in</strong>g on the exception class. This shell dialog demonstrates the<br />

different cases:<br />

1> catch throw(foo).<br />

foo<br />

2> catch exit(foo).<br />

{'EXIT',foo}<br />

3> catch foo=bar.<br />

{'EXIT',{{badmatch,bar},[{erl_eval,expr,3}]}}<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!