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.

56<br />

2.8.2 – Us<strong>in</strong>g try…catch<br />

In modern <strong>Erlang</strong>, you use a try-expression to h<strong>and</strong>le exceptions that may occur <strong>in</strong> a piece<br />

of code. In most ways it works like a case-expression, <strong>and</strong> <strong>in</strong> the simplest form it looks like<br />

this:<br />

try<br />

some_unsafe_function(…)<br />

catch<br />

oops<br />

-> got_throw_oops;<br />

throw:Other -> {got_throw, Other}<br />

exit:Reason -> {got_exit, Reason}<br />

error:Reason -> {got_error, Reason}<br />

end<br />

Between try <strong>and</strong> catch, we have the body or protected section. Any exception that<br />

occurs with<strong>in</strong> the body <strong>and</strong> tries to propagate out from it will be caught <strong>and</strong> matched aga<strong>in</strong>st<br />

the clauses listed between catch <strong>and</strong> end. If it doesn’t match any of the clauses, the<br />

exception will cont<strong>in</strong>ue as if there was no try-expression at all around the body. Similarly, if<br />

the body is evaluated without rais<strong>in</strong>g an exception, its result will become the result of the<br />

whole expression, as if the try <strong>and</strong> catch…end had not been there. The only difference is<br />

when there is an exception, <strong>and</strong> it matches one of the clauses. In that case, the result<br />

becomes whatever the match<strong>in</strong>g clause returns.<br />

The patterns of these clauses are a bit special—they may conta<strong>in</strong> a colon (:) to separate<br />

the exception class (error, exit, or throw) from the thrown term. If you leave out the<br />

class, it defaults to throw. You shouldn’t normally catch error <strong>and</strong> exit unless you know<br />

what you are do<strong>in</strong>g—it goes aga<strong>in</strong>st the idea of fail<strong>in</strong>g early, <strong>and</strong> you could be mask<strong>in</strong>g a real<br />

problem. Sometimes, though, you just want to run some code that you don’t trust too much,<br />

<strong>and</strong> catch anyth<strong>in</strong>g that it throws. You can use the follow<strong>in</strong>g pattern for catch<strong>in</strong>g all<br />

exceptions:<br />

_:_ -> got_some_exception<br />

(or “Class:Term -> …” if you want to <strong>in</strong>spect the data <strong>in</strong> the exception).<br />

Also, note that once you get to the catch part, the code is no longer protected. If a new<br />

exception happens <strong>in</strong> a catch-clause, it will simply propagate out of the entire tryexpression.<br />

2.8.3 – try…of…catch<br />

There is a longer form of try that is useful when you need to do quite different th<strong>in</strong>gs <strong>in</strong> the<br />

successful cases <strong>and</strong> the exception cases. For <strong>in</strong>stance, if you want to cont<strong>in</strong>ue do<strong>in</strong>g some<br />

work with the value you got <strong>in</strong> the successful case, but want to pr<strong>in</strong>t an error message <strong>and</strong><br />

give up <strong>in</strong> the exception case, you can add an “of…” section, like this:<br />

try<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!