On Thursday, 8 September 2016 at 11:40:17 UTC, Russel Winder wrote:
Is the fact that:

    void f() nothrow {
            assert(1 == 0);
    }

    int main() {
            f();
            return 0;
    }

compiles fine but at run time f does indeed throw an exception what should happen? If it is what does nothrow actually mean?

To expand on the previous correct answers, nothrow is about not throwing Exceptions. Exceptions are part of the normal flow of the program: they are used to signal recoverable errors, much like error codes.

Errors, on the other hand, are unrecoverable system failures that should not be catched (catching them is undefined behaviour, I think) and will surely lead to a crash. Errors are not covered by nothrow.

Asserts throw Errors, not Exceptions. The reason is that asserts are used to test conditions that must hold, at the point that an optimizing compiler can analyze the expression inside the assert and optimize the executable based on the truthness of that condition. So if an assert is found false at runtime, the code may not be able to work at all. That's why asserts throw Errors and not Exceptions.

Reply via email to