I have recently gotten into a discussion over raise
vs. throw
. There is another SO thread discussing this, and the pundits summarized the situation as this:
- Throw and raise are, to a great degree, interchangeable
- Advantage of
throw
is that you can throw easily anything, and that you don't waste time putting together the stack trace
That leaves me with a question, why nobody is actually using throw in Ruby? The thread discussed earlier mentioned use of catch / throw in Sinatra to handle HTTP error codes, but still, I have seen very few catch / throw examples in the wild, and I only used it in my own code once. As current Rails security crisis has shown, Ruby community widely ignores important features ($SAFE level in this particular case). So what is the thing with catch / throw?
来源:https://stackoverflow.com/questions/16972757/why-is-throw-and-catch-hardly-ever-used-in-ruby