Re: Unhandled Exception Question
On 4 oct, 05:21, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
I looked at vector's header. I only see that there i=
s only throw in
some functions, but they don't have try / catch block.
If try / catch block is not written, then throw will be e=
xecuted 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.
If you want to use exception handler, you will include ex=
ception's
header.
Not necessarily. You can also catch all exceptions:
catch(...){
// all exceptions
}
For example
#include <exception>
try {
vector< int > v;
v.push_back( 1 );
v.push_back( 2 );
v.push_back( 3 );
v.at( 4 ) = 5;}
catch ( exception &e ) {
cerr << =93Error: =93 << e.what() << endl;
}
Is my example above correct?
Apart from the missing 'main' enclosing the code, missing #include
<vector> and I assume a using namespace std somewhere; yes.
It triggers an exception handler to
report that subscript is out of range.
It triggers an exception and search for a matching exception handler.
Upon entering the exception handler, the stack is unwound.
Without try / catch block, unhandled exception is always =
triggered
when at() throws subscript out of range.
It triggers an exception and search for a matching exception handler
and doesn't find any. You would also have an unhandled exception with
the following code:
try {
vector< int > v;
// ...
v.at( 4 ) = 5;
}
catch ( int e ) {
cerr << =93Error: =93 << e << endl;
}
It is implementation defined whether stack is unwound when calling
std::terminate().
By the way, there is no unhandled exception mechanism to trigger.
AFAIK the message you have is part of the implementation of your
compiler and is not mentioned in the standard.
--
Michael