4 ms·
> Python, C++, Ruby and Java all use exceptions. To them, “exception” means, “nifty piece of syntax kinda like goto.” Not Ruby. In idiomatic Ruby^ exceptions a
by lloeki 2y ago
> Python, C++, Ruby and Java all use exceptions. To them, “exception” means, “nifty piece of syntax kinda like goto.”
Not Ruby. In idiomatic Ruby^ exceptions are for exceptional cases, not code flow. "nifty piece of syntax kinda like goto." is the much underused but very powerful catch+throw.
Or rather, it's a stack unwinding mechanism: think baseball, the catcher expects a ball, with the ball being a message to be passed. Upon that you could implement a specialized and more costly case of stack unwinding which does what fail/raise+rescue does, but catch+throw is more general than exceptions.
Take Python's StopIteration as an example: there's absolutely no need for this to be a full exception, instead it's like `break`ing a loop but across method calls.
^ Nevermind Rails practices. Rails is not Ruby.
- LtWorf 2y ago[1]: isinstance(StopIteration, Exception) [1]: False
- _ZeD_ 2y agoyeah.. StopIteration is not an instance of Exception, is a subclass of it :^) no, really: help(StopIteration) class StopIteration(Exception) | Signal the end from iterator.__next__(). | | Method resolution order: | StopIteration | Exception | BaseException | object ...