Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ have a way to ignore an exception from a function?

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?

like image 871
TheArchitect Avatar asked Apr 16 '17 13:04

TheArchitect


2 Answers

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
like image 179
David Haim Avatar answered Oct 11 '22 01:10

David Haim


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.

like image 32
Lightness Races in Orbit Avatar answered Oct 11 '22 01:10

Lightness Races in Orbit