[erlang-questions] is this try expression idiom common?

Kevin <>
Tue Nov 11 21:56:36 CET 2008


Whether its the pragmatic book or the online docs, the impression I've 
always gotten about try..catch expressions is this

try FuncOrExpressionSequence of
Pattern1 [when Guard1] -> Expressions1;
Pattern2 [when Guard2] -> Expressions2;
catch
ExceptionType1: ExPattern1 [when ExGuard1] -> ExExpressions1;
ExceptionType2: ExPattern2 [when ExGuard2] -> ExExpressions2;
end


In fact the pragmatic book calls try...catch "like a case expression on 
steroids. It’s basically a case
expression with catch and after blocks at the end."

So, to a newbie like me, I've always assumed you needed case like 
pattern matchers inside the try. This was seriously cramping my style. 
I've just discovered you can simply do this


doit() ->

try
a(),
b(),
c()
catch
throw:_ -> something_failed
end.



a() -> ok.
b() -> throw(not_ok).
c() -> ok.


I guess this creative formatting of

try a(), b(), c()
catch
throw:_ -> something_failed
end.


To me this is a completely different idiom, absolutely nothing like a 
case expression, and opens erlang up to a more exception based control 
flow and error handling. Is this legit? Frowned upon? I searched the 
pragmatic book and there is not a single example of this.








More information about the erlang-questions mailing list