Ian Collins wrote:
A boiled down (no framework) example:
#include <iostream>
#include <exception>
#include <assert.h>
struct AssertionException : std::runtime_error
{
const char* expression;
const char* file;
int line;
AssertionException(const char* expression, const char* file, int line)
: std::runtime_error( expression ),
expression(expression), file(file), line(line) {}
};
void __assert(const char* expression, const char* file, int line)
{
throw AssertionException( expression, file, line );
}
I'm not a complete expert, nor am I a sulky person, but before writing
assert exceptions one might consider following that I found in the
book C++ Coding Standards by Sutter & Alexandrescu, Item 68 - Assert
liberally to document internal assumptions and invariants.
Quote:
"It is not recommended to throw an exception instead of asserting,
even though the standard std::logic_error exception class was
originally designed for this purpose. The primary disadvantage of
using an exception to report a programming error is that you don't
really want stack unwinding to occur - you want the debugger to launch
on the exact line where the violation was detected, with the line's
state intact."
assertions trigger when expected. It was not a suggestion for
function to terminate. It will blunder on with invalid inputs and do
nasty things. In this instance, mapping assertions to exceptions is the