[erlang-questions] feedback please
Thu Sep 24 07:56:18 CEST 2015
On Thursday 24 September 2015 07:41:40 Roelof Wobben wrote:
> You are right. I did take a quick look and almost the whole chapter is
> try .. catch. So match or chrash is better ?
It is not so much of a matter of which is "better" -- but that *most* of the time you want to follow the mantra of "let it crash" and this is a very natural fit for Erlang. For example, if I start a process that is supposed to read some configuration settings in its init/1 section before it gets going, and the config data is all screwed up (wrong format, maybe its not even the right data -- like binary image data instead of a settings file or something) then I don't want to try..catch and then write extra code to force the process into some semi-workable state and carry on. I want to just crash right away.
In development this might just be something that explodes in my terminal. BOOM! And then I know I need to look at something right away:
"Why the devil am I sending image data where a file name is supposed to go?"
In production it may be an indicator that some remote resource you normally query has changed its API:
"Oh boy, our [very important to your business] service changed their protocol unannounced... again...".
In either case the last thing you want to do is have the processes silently carry on and crash *other* processes (or other services!) *way later* for reasons that are now at least one degree removed from the actual problem.
But occasionally you will want a try..catch. Sometimes you just want to be able to stay alive long enough to log a very specific/detailed error in a natural way; sometimes you are dealing with an unknown platform and interacting with a user at the same time, and you're not sure what sort of data you might receive -- but if the data is bad you need to stay alive long enough to tell the user about it in detail. In cases like these you *can* accomplish this without try..catch, but its really annoying. In most cases, though, try..catch is much more annoying (and downright dangerous compared to crashing immediately).
In my experience try..catch is something you'll know when you need, and that need is infrequent. Almost any time you use try..catch you are not writing for the "success case" (or "happy path") -- the rare times that try..catch fits into the "happy path" not having it would be an annoying handicap.
So its not that "crashing is better than try..catch" so much as "standard Erlang practice is to crash on faults, and try..catch is a very useful tool in some specific cases", so you do need to know about it.
More information about the erlang-questions