Re: Unhandled Exception Question
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 there=
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.
Let me understand clearly.
void foo( int index ) {
if ( index < 0 || index > 15 )
throw;
}
This function does not have try / catch block. If throw is executed,
then terminate() function is called. Correct?
Maybe you suggest to put terminate() function inside foo() function
to replace throw. You can write your own terminate() function and
uses set_terminate().
If you want to use exception handler, you will include =
exception'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.
Please clarify what you mean stack is unwind? If exception handler
in the C++ Compiler's setting is turned off, how do exception handler
work?
Without try / catch block, unhandled exception is alway=
s 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- Hide quoted text -
- Show quoted text -