Re: Place Assert in Exception
On 10/10/10 10:15 AM, Immortal Nephi wrote:
You can write your own assert macro. I have seen that assert macro
always use either abort() or terminate(). abort() causes serious
problems if it does not call class? destructor to deallocate memory.
Memory leak is getting worse and the performance is dropped from the
operating system while you call abort() many times.
It won't if the OS is doing its job.
Exception handler is the best option over abort(). You can write
your own message box in either windows or console.
Assuming you have either.
I write Error_Report class. Error_Report class is different from
exception class because it does not require inheritance. All the data
members and member functions of Error_Report have static storage.
Exceptions don't require inheritance, what's your point?
Static storage is required. If you do not declare static, then after
throw is invoked, throw calls Error_Report?s constructor and
destructor before both data members pop out of stack and going to the
catch block.
You can add error messages to the enum block. You use assert macro
to select error message.
I have seen many arguments against placing exception in assert.
My code looks very clear. You can always customize message box, but
assert macro is not changed.
But what it is built on a fallacy, namely "abort() causes serious problems".
#include<iostream>
#include<string>
#include<sstream>
class Error_Report {
public:
enum Code {
OUT_OF_RANGE,
POINTER_NULL,
NORMAL
};
enum Behavior {
e_ABORT,
e_RETRY,
e_IGNORE,
};
Error_Report( Code _code ) {
code = _code;
}
~Error_Report() {
}
static char* What() {
return Error_Report::message[ code ];
}
static Behavior Assert(
Code _code,
const char* File,
const int LineNumber,
const char* Function ) {
std::ostringstream text;
text<< "Debug Assertion Failed!\n\nFile: "<< File<<
"\nLine: "<< LineNumber<< "\nFunction: "<< Function<<
"\n\nExpression: "<< message[ _code ]<<
"\n\nFor information on how your program can cause a report\n"<<
"failure, see the Visual C++ documentation on asserts.\n\n";
Why would I want to "see the Visual C++ documentation on asserts"?
--
Ian Collins