Herb Sutter delivered a talk at ACUU Conference regarding the future of exceptions in C++ and upcoming contracts to replace or enhance existing assert.
He postulates the following rules to deal with error handling:
System corruption (e.g., stack overflow): terminate
Programming bug (e.g. precondition violation): assertions, contracts
Recoverable error (e.g., network down): exception, error code
While I agree, I am not using this approach and essentially combine the 2 and 3 into one. I do that because I do not know how to test assertions or the upcoming contracts. Both use similar mechanisms that their violation causes the program to terminate and call optional handlers before that.
From the documentation (emphasis is mine):
A program may be translated with one of two violation continuation modes:
- off (default if no continuation mode is selected): after the execution of the violation handler completes, std::terminate is called;
- on: after the execution of the violation handler completes, execution continues normally.
Implementations are encouraged to not provide any programmatic way to query, set, or modify the build level or to set or modify the violation handler.
This means that test programs that will be tested against the contracts will require an extra compilation switch. While I dislike this, I understand why it would be done this way. More worrying is the last part, and the question about it has been raised. If the ability to set our own contract violation handler is implementation-defined, it will not be consistent across toolchains. Any tests of the contracts will, therefore, not be portable if at all possible. Setting it as yet another parameter to the linker would be quite awkward as well (and definitely not portable).
Currently, testing against assert
is not possible because it simply aborts the program, and the custom handler cannot prevent that as far as I know. With contracts, this was supposed to be possible using the custom handler and the build switch, but as it turns out, it might not be possible as well (or be implementation-defined).
Or is there any other way testing against asserts and contracts is possible that I am missing?