Re: Unhandled Exception Question
On 5 oct, 04:52, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
On Oct 4, 5:30 am, Michael Doubez <michael.dou...@free.fr> wrote:
On 4 oct, 05:21, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
I looked at vector's header. I only see that the=
re is only throw in
some functions, but they don't have try / catch block.
If try / catch block is not written, then throw will =
be executed to
the operating system directly to trigger unhandled exception.
Correct?
Not exactly.
If no handler is found, std::terminate() is called.
It the exception if thrown from a object's constructor/destructor
whith static storage duration, the exceptions are not caught.
[snip - answered elsethread]
It triggers an exception and search for a matching exception handler.
Upon entering the exception handler, the stack is unwound.
Please clarify what you mean stack is unwind? If excep=
tion handler
in the C++ Compiler's setting is turned off, how do exception handler
work?
When the exception is thrown, the code looks for a exception handler:
i.e. a try block it is executed from with a catch close taking as
parameter a type matching the type of the exception thrown.
If none is found meaning no try block or no matching handler up to the
execution entry point (main() or static initialisation) then
std::terminate is called. The objects with automatic lifetime are not
necessarily destroyed upon entering std::terminate(), this is
implementation defined.
If one handler is found. The objects with automatic duration (i.e.
living on the stack) will be destroyed (kind of pop out of the stack
sequentially) up to the beginning of the try block that have a
matching handler. Then the execution jumps to the code of the catch
clause with a matching parameter (the handler).
As an example:
#include <iostream>
#include <cassert>
struct Automatic
{
Automatic(std::string const & name):m_name(name)
{ std::cout<<"Constructor of "<<m_name<<std::endl;}
~Automatic()
{ std::cout<<"Destructor of "<<m_name<<std::endl;}
std::string m_name;
};
Automatic staticObject("'Static Object'");
void foo()
{
Automatic inFoo("'Object in foo'");
throw "string exception";
Automatic neverHere("'Object never constructed'");
}
int main(int argc)
{
Automatic beforeTry("'Object before try'");
// caught exception
try {
Automatic inTry("'Object in try'");
foo(); // throws char const *
Automatic afterFoo("'Nothing executes after foo'");
}
catch(int a)
{
assert( false && "parameter does not match" );
}
catch( char const * str)
{
Automatic inCatch("'Object in catch'");
}
if( argc == 2 )
{ // uncaught exception
try {
Automatic inUncaught("'Object before uncaught'");
throw "uncaught exception";
}
catch(int a)
{
assert( false && "parameter does not match" );
}
}
}
Calling the program without uncaught exception you will notice that
'Object in try' is destroyed before entering catch().
# ./test
Constructor of 'Static Object'
Constructor of 'Object before try'
Constructor of 'Object in try'
Constructor of 'Object in foo'
Destructor of 'Object in foo'
Destructor of 'Object in try'
Constructor of 'Object in catch'
Destructor of 'Object in catch'
Destructor of 'Object before try'
Destructor of 'Static Object'
Calling the program with uncaught exception, you will notice that
'Object before uncaught', 'Object before try' (and 'Static Object' for
that matter) never get destroyed on my compiler with my compilation
options.
# ./test z
Constructor of 'Static Object'
Constructor of 'Object before try'
Constructor of 'Object in try'
Constructor of 'Object in foo'
Destructor of 'Object in foo'
Destructor of 'Object in try'
Constructor of 'Object in catch'
Destructor of 'Object in catch'
Constructor of 'Object before uncaught'
terminate called after throwing an instance of 'char const*'
Aborted (core dumped)
[snip]
--
Michael