[Edit: The title of this thread should have been "The opposite of noexcept". Sorry about that.] (fixed: Salem)
I have a variety of Die()-type routines that I use for stack tracing of exceptions and so forth. Typical usage would be something like
Die() builds an exception object from the error message, and from the information in HERE (which is just a macro that expands to __LINE__, __FILE__, __PRETTY_FUNCTION__). It then throws the exception.Code:int myfunc() { if (condition) { return 0; } else { Die("error message", HERE); } }
There is of course a problem with this setup. Since myfunc() and Die() are defined in different translation units, the compiler has no way of knowing that Die() always throws an exception. Accordingly (since we're using the -Wall option) it will grumble that the else block of myfunc() doesn't return an int value.
There are a few simple ways of addressing this, such as:
- adding a "throw" statement after Die() in the else block;
- putting a default return value at the end of myfunc();
- using -Wno-return-value in the compiler options list.
At first, I didn't like options 1 and 2. So I used option 3 instead. Boy, did I end up regretting that one.
So now my code is now full of dummy throw and return statements that don't actually do anything (and could act as red herrings during debugging), but which have to be there to keep the compiler from throwing tons of warnings about things that can't happen.
Is there any way of letting the compiler know that it doesn't need to worry about missing return values from logic branches that contain calls to Die()?
Equivalently, is there any way of letting the compiler know that Die() always throws an exception? This would be the same as having an "alwaysexcept" specifier, the opposite of "noexcept".