What are the common usage of exceptions at catch site?
c++, c++11, exception, try-catch
Solution
This is very big subject.
I doubt you can easily translate third party exceptions into your own exceptions, and moreover personally I see no need to implement this behavior. Since 3rd party library is a part of your implementation, which is not exposed to the public API, why would you expose all of its exceptions (even through some mapping)? If one day you stick to another 3rd party library implementing the same stuff - would you like to redesign whole exception hierarchy? I think not. API of your library must not be fragile, so I would propose not to map external exceptions to your own ones.
You can map 3rd party exceptions to your hierarchy following ways:
Don't wrap anything at all. I mean you don't have to throw anything just because 3rd library does so. You can catch that exception and handle it, or return error code, or change the state appropriately. There are many other possibilities rather than rethrowing always.
You don't have to make one-to-one translations for all 3rd party exceptions. If you use library AAA internally, then you can have single AAAException representing many exceptions coming from that library.
Valuable to know: always catch exceptions by const reference:
`catch (const exception & ex)`
This subject is very big, I hope my answer helps to understand it.
Answers to the comments:
If I do not map third party exceptions to my own API (need not to be one to one), they leak to client code - No, they don't, that's the whole point! You must catch them inside your library and then decide what to do with catched exceptions: throw your own exception, return error code, notify client listener, log error etc...
try {
3rdpatry.call();
} catch (const 3rdpartyException & ex) {
// throw YourException(ex.what());
// listener.notify(some_error)
// return some_code
}
Catching by const reference is not to prevent slicing at all. There is nice discussion here that explains that.
Problem
My understanding about exception handling is very limited. While I find it is easy to throw an exception (or I can pack it using `expected<T>` for later consumption), I have very little idea about what to do with an exception. Presently my knowledge is limited to clean my own resources and rethrow the exception to be handled at appropriate location. e.g. ``` ptr p = alloc.allocate(n); try { uninitialized_copy(first,last,p);//atomic granularity, all or none } catch(...) { alloc.deallocate(p,n); throw; } ``` But I guess, this can be equivalently transformed in a `RAII` pattern as ``` alloc_guard<ptr> p{alloc.allocate(n)}; uninitialized_copy(first,last,p.get()); p.commit(); ``` catch the exception at a top level, compose & print a nice message and exit.e.g. ``` int main(int argc,char** argv) { try { app_t the_app(argc,argv); the_app.run(); } catch(std::runtime_error& e) { //instead of what, I can also compose the mesage here based on locale. std::cout<<e.what()<<std::endl; } } ``` So, all I do is in a top level function such as `main` catch the exception and print an appropriate message and close. While implementing a library with a nice set of API using various external libraries as back end for implementation, I realized that third party library exceptions are part of my API specification, as they cross my library boundary and land in user code! So, my library API leaked all exceptions from external libraries (and each one having their own exception hierarchy) that I was using to the user code. This leads to my question, what all can be done when I catch any exception ? More specifically, - Can I translate the caught exception from external library to my own exception and throw that in a generic way (say the mapping between third party library exception hierarchy and my exception API is provided as a `mpl::map`) ? - Can I do something more useful than printing a message/call stack, say resume the function at the throw site with different input parameter (say when I get that a `file_not_found` or `disk_error`, re-run the function with a different file)? - Any other pattern which is valuable to know? Thanks