I created a function that throws an exception, but under some circumstances I want it to simply ignore this exception.
I wrote my code like this, but it's not quite elegant:
try {
myFunction();
} catch (...) {}
Does C++ another way to write this?
No, there isn't.
you can follow what the standard does in this case which is to overload the function twice, once with std::nothrow_t and once without. use the later to wrap the first
std::error_code my_function(std::nothrow_t) noexcept;
void my_function(); //throws
No, that is how you would write it.
It's not bad in and of itself, though if you find that your code is becoming ugly due to the number of times you're employing this construct, that may be a signal that you're employing it too much.
I find myself ignoring exceptions occasionally, but if it's the "norm" for you then something may be wrong with your design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With