3 ms·
The function try blocks is new to me. It's actually kind of elegant (for C++). Granted, the reason I've probably never encountered it in the wild is that ther
by kenrose 13y ago
The function try blocks is new to me. It's actually kind of elegant (for C++). Granted, the reason I've probably never encountered it in the wild is that there are probably very few scenarios where you have a member in the initializer list where you want to handle an exception on initialization. If you were trying to handle an exception of an instance variable initializing, most people would just move that member to the constructor body (e.g., if you have a pointer). The only time this would be useful then is for members that don't have default initializers (e.g., a reference).
- evincarofautumn 13y agoIt also just removes a level of nesting, which is nice by itself.
- b-johansson 13y agoFunction try blocks are also extra tricky because at the end of the catch block the exception is automatically rethrown. http://www.drdobbs.com/sutters-mill-constructor-failures-or-the/184401316 http://www.drdobbs.com/sutters-mill-constructor-failures-or-... has a lot of details on function try blocks.
- jasonzemos 13y agoIt's nice to avoid the default initialization and construct a member directly with the argument you want. Then you can construct a second member which might depend on that previous one, etc. This can even include lambdas right in the initialization list, and all of this may throw and log an error with the catch block. Personally, I rarely like to see code in the actual constructor body anymore.