Re: Place Assert in Exception
On Oct 9, 4:23 pm, Ian Collins <ian-n...@hotmail.com> wrote:
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 wr=
ite
your own message box in either windows or console.
Assuming you have either.
I write Error_Report class. Error_Report class is different f=
rom
exception class because it does not require inheritance. All the dat=
a
members and member functions of Error_Report have static storage.
Exceptions don't require inheritance, what's your point?
No, it is not correct. Exception class has inheritance. Look at
stdexcept header. Several classes are derived from exception class.
For example, you call out_of_range() before the constructor in turn
calls exception constructor. exception class has virtual functions.
Static storage is required. If you do not declare static, the=
n 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 bo=
x, but
assert macro is not changed.
But what it is built on a fallacy, namely "abort() causes serious problem=
s".
#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++ doc=
umentation on asserts.\n\n";
Why would I want to "see the Visual C++ documentation on asserts"?
Like I said before. You can customize Assert function of
Error_Report class. You add platform specification macros. It can go
to Windows, Linux, Mac OSX, or other operating system. Also, you can
tell which C++ Compiler is Microsoft or GCC or Intel.
You do customize text message. Abort, Retry, Ignore option is not
changed.
Please tell me what you think Error_Report class? Is throw better
than abort()?