void MyFunction(int i) throw();
it just tells the compiler that the function does not throw any exceptions.
It can’t make sure the function throw nothing, is that right?
So what’s the use of throw()
Is it redundant? Why this idea is proposed?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, when the compiler works right, it is enforced — but at run-time, not compile-time.. A function with an empty exception specification will not throw an exception. If something happens that would create an exception escaping from it, will instead call
unexpected(), which (in turn) callsabort. You can useset_unexpectedto change what gets called, but about all that function is allowed to do is add extra “stuff” (e.g. cleanup) before aborting the program — it can’t return to the original execution path.That said, at least one major compiler (VC++) parses exception specifications, but does not enforce them, though it can use empty exception specifications to improve optimization a little. In this case, an exception specification that isn’t followed can/does result in undefined behavior instead of necessarily aborting the program.