|To be honest, I have no idea what "exception safe" means. Maybe "for every throw there is a catch"? |
An exception safe class is one that is guaranteed to clean up any resources that it allocates. Exception-safe code uses only such classes, and never contains a "naked" new, malloc, file open, or anything else that requires a matching close/free.
For example, if the class has a file handle, the destructor closes the handle; if the class allocates memory, the destructor frees memory. Because this can be hard to get right 100% of the time, you build such classes from simple primitives, such as auto_ptr, shared_ptr, and a self-closing file handle. Ideally, your destructor should have nothing to clean up, as all member variables should be self-cleaning.
Putting classes like that on the stack (local variables) is exception safe. There is never any "clean-up" code at the bottom of a function - ever. Since all classes clean themselves up, you never have to care whether a function exits normally, exits with error, or something it calls throws.
|Having execution safe code (entering a try block) is extremely slow. |
I believe the total cost in the MS compiler of entering a try block is pushing one more argument on the stack. I'd be interested to see evidence of "extremely slow". Unwinding the stack on a throw can be costly, of course, but no mor so than returning normally "up the stack" would have been.
In any case, if you have lots of try/catch blocks, you're doing it wrong. The only places you should catch exceptions are at a "formal API boundary" (such as a dll boundary) or in those rare cases that you can actually do something about the problem that caused the exception.