Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a C++ function that catches all exceptions inside it "noexcept"?

Tags:

c++

A have a conceptual question. Consider a C++ function that catches all possible exceptions thrown by code it calls, it looks like

void func() {
    try {
        // lots of stuff that may throw
    } catch(...) {
        // handle exceptions, NOTHING here throws.
    }
}

My question is: Does this function qualifies as noexcept? And should it be declared as such? I have this doubt because there is a possible exception propagation going on inside it, but caller code will never receive one.

like image 762
Elvis Teixeira Avatar asked Oct 27 '25 05:10

Elvis Teixeira


1 Answers

From Exception specifications, Section 15.4/2 of the C++11 standard:

A function with a non-throwing exception-specification does not allow any exceptions.

If the code in the catch block is guaranteed to not throw any exceptions, then the function can have a non-throwing exception-specification. i.e. you may use:

void func() throw () { ... }

or

void func() noexcept { ... }

or

void func() noexcept(true) { ... }
like image 73
R Sahu Avatar answered Oct 28 '25 19:10

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!