Discussion:
what happen behind the scences for the following exception?
(too old to reply)
w***@gmail.com
2008-12-04 02:20:47 UTC
Permalink
For the purpose of practicing C++ exception handling, I invented a
trivial program as follows:
class SE_Exception {
private:
SE_Exception() {}
unsigned int nSE;

public:
SE_Exception( SE_Exception& e) : nSE(e.nSE)
{
puts("SE_Exception Copy Constructor");
}
SE_Exception(unsigned int n) : nSE(n)
{
puts("SE_Exception Constructor");
}
SE_Exception& operator=(SE_Exception& e)
{
nSE = e.nSE;
puts("SE_Exception assignment operator");
return *this;
}
bool operator==(SE_Exception& e)
{
puts("SE_Exception equality comparison operator");
return nSE==e.nSE;
}
~SE_Exception()
{
puts("SE_Exception Destructor");
}
};

int main() {
_set_se_translator( trans_func );
try {
SE_Exception* e = new SE_Exception(94);
throw e;
}
catch( SE_Exception e ) {
printf_s( "Caught a __try exception with SE_Exception.\n" );
}
}

I run the program and find that there are two exceptions. The first
one is an unhandled exception, which meets my expectancy. The second
one, however, surprisingly, is an exception with respect to kind of
buffer-overflow. Could anyone explain the details? Please!
w***@gmail.com
2008-12-04 03:01:13 UTC
Permalink
What's more, the type of thrown exception is a pointer which is an
integer. Thus the exception object is an integer and the expression in
catch clause should evaluate to false, which means the compound
statements in catch block should not be executed. To my dismay, the
flow of control enter the compound statements.I thought the
constructor in SE_Exception taking an unsigned int argument may taking
an implicit conversion. I added an "explicit" keyword before that
constructor and even delete the constructor. But neither could take
effects, the two exceptions still remain.
Why,again?
Alex Blekhman
2008-12-04 12:19:46 UTC
Permalink
Post by w***@gmail.com
What's more, the type of thrown exception is a pointer which is
an integer. Thus the exception object is an integer and the
expression in catch clause should evaluate to false, which means
the compound statements in catch block should not be executed.
To my dismay, the flow of control enter the compound
statements.I thought the constructor in SE_Exception taking an
unsigned int argument may taking an implicit conversion. I added
an "explicit" keyword before that constructor and even delete
the constructor. But neither could take effects, the two
exceptions still remain.
Why,again?
I reckon you run the program under debugger. Debugger can alter
program behavior because it attemps to catch an uncaught exception
and give you the opportunity to debug. If you run your program
without debugger, then you will see that once SE_Exception is
thrown and not caught, the program immediately terminates (via
call to `terminate' function).

Alex
w***@gmail.com
2008-12-04 13:11:58 UTC
Permalink
Alex, you are right. I did run the program under debugger and the
results from running without debugger was just as what you said.
Thank you very much.
By the way, what behavior the debugger would take in which situation?
Alex Blekhman
2008-12-04 14:55:44 UTC
Permalink
Post by w***@gmail.com
By the way, what behavior the debugger would take in which
situation?
It is hard to tell, because unhandled exceptions and debugger
tread on each other's toes. Ideally, I would like to see the same
behaviour under the debugger as without the debugger. But in
practice debugger often introduces minor changes. It is true for
exceptions, multithreading, operations where timing is important
etc. Sometimes debugger cannot be used at all because it disturbs
the regular execution flow too much.

Alex

Continue reading on narkive:
Loading...