Hi Matt,
On Tuesday, July 17, 2012 10:36:48 PM UTC+2, Matt Green wrote:
I have a few questions on exceptions.
1. How are they implemented in the runtime? (I assume they work in the same way as Obj-C, and that they're fairly expensive.)
Ruby exceptions are implemented as C++ exceptions in the runtime. So yes, there are fairly expensive to throw and catch. However, these are often called zero-cost exceptions because "try" scopes are almost free to enter (the runtime does not need to setup anything before entering a begin block).
2. How should they be used? I know Obj-C programmers tend not to use them very much, except as a last resort (e.g. preconditions violated).
It's really better not to use exceptions at all, since they can dramatically impact the performances of an app. However, if you use exceptions for... well, exceptional code paths (right before crashing the app for example), I guess it should be okay. But it's better to avoid them when doing simple error checking.
3. If they can be used, what base classes should be used?
You can follow the Ruby principles here (I guess you need to subclass Exception but I'm not sure.)
Laurent