Exceptions

112 views
Skip to first unread message

Matt Green

unread,
Jul 17, 2012, 4:36:48 PM7/17/12
to rubym...@googlegroups.com
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.)
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).
3. If they can be used, what base classes should be used?

Thanks,
Matt

Laurent Sansonetti

unread,
Jul 19, 2012, 5:06:38 AM7/19/12
to rubym...@googlegroups.com
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 

Steve Ross

unread,
Jul 20, 2012, 1:24:58 PM7/20/12
to rubym...@googlegroups.com
C++ exceptions? Don't they have to preserve all the stack frames on entry so they can unwind in reverse order when an exception is thrown? That would be a memory hit, no? Just out of curiosity, why C++ exceptions? Do you rely on destructors being called in reverse order, as was the intention of these exceptions?

Steve

--
 
 
 

Reply all
Reply to author
Forward
0 new messages