My understanding of exception handling is very limited. Although I find it easy to throw an exception (or I can pack it with expected<T> for later consumption), I know very little about what to do with the exception.
Currently my knowledge is limited
clear my own resources and rebuild the exception that will be handled in the appropriate place. eg.
ptr p = alloc.allocate(n); try { uninitialized_copy(first,last,p);//atomic granularity, all or none } catch(...) { alloc.deallocate(p,n); throw; }
But, I think it can be equivalently converted to a RAII pattern as
alloc_guard<ptr> p{alloc.allocate(n)}; uninitialized_copy(first,last,p.get()); p.commit();
catch the exception at the top level, compose and print a nice message and exit.eg
int main(int argc,char** argv) { try { app_t the_app(argc,argv); the_app.run(); } catch(std::runtime_error& e) {
So, everything I do is in a top-level function, for example main , catches the exception and prints the corresponding message and closes.
When implementing a library with a good set of APIs that uses various external libraries as the back end for implementation, I realized that third-party library exceptions are part of my API specification as they cross my library boundary and fit into user code!
So, in my library API all exceptions from external libraries are registered (and each of them has its own hierarchy of exceptions), which I used for the user code.
This leads to my question, that everything can be done when I catch any exception?
More specific,
- Is it possible to translate a caught exception from an external library into my own exception and throw it in a general way (say, a comparison between the hierarchy of exceptions of third-party libraries and my exception API is provided as
mpl::map )? - Can I do something more useful than printing a message / call stack, say, resuming a function on a throw site with a different input parameter (say, when I get this
file_not_found or disk_error , re-run the function from another file)? - Any other template worth knowing?
thanks
source share