mercredi 10 novembre 2021

Calling a function that throws exceptions from a noexcept function

I am using a library from a 3rd party whose source code I have no control over. The library has a function (say foo()) which may throw exceptions under certain conditions.

If the function (in my code) that I am calling foo from can guarantee that the conditions under which foo() will throw will not be met presently would it make sense for me to declare my function noexcept and thus save on the compiler unwinding routines and thus make the code faster?

I have not done any performance testing to see if it would be faster. I am just checking in to see if this is a good design. The reason why I ask this is because in future the library code could change and possibly throw exceptions in cases that I may not have handled in my code. If this happens then my application would terminate. So from a design perspective is what I am thinking of doing a good move? Or should I just handle the exceptions in my code and not propagate them out and leave the function as noexcept? The callers of my function wouldn't be able to do anything meaningful with these exceptions. So I don't want to propagate them out or wrap them into an application specific exception either.

Aucun commentaire:

Enregistrer un commentaire