unexpected behaviour of set_unexpected()

B

bb

Hi,
I am using gcc v4.0.2 on fedora core 4 (2.6.16). Any reason why the
handler set thru' set_unexpected() never gets called in the following
code?

--------- Code -------------

#include <iostream>
#include <exception>
#include <string>

void myHandler();
int main(int argc, char** argv);
int main(int argc, char** argv) {

std::set_unexpected(myHandler);

try {
throw std::string("This is an exception");
} catch (std::exception& ex) {
std::cout << "Expected exception caught" << std::endl;
}

}

void myHandler() {
std::cout << "Unexpected exception caught" << std::endl;
throw std::exception();
}

-------- Produces the following Output -------------

terminate called after throwing an instance of 'std::string'
Aborted

-------- End -----------------
 
B

Bo Persson

bb said:
Hi,
I am using gcc v4.0.2 on fedora core 4 (2.6.16). Any reason why the
handler set thru' set_unexpected() never gets called in the
following
code?

--------- Code -------------

#include <iostream>
#include <exception>
#include <string>

void myHandler();
int main(int argc, char** argv);
int main(int argc, char** argv) {

std::set_unexpected(myHandler);

try {
throw std::string("This is an exception");
} catch (std::exception& ex) {
std::cout << "Expected exception caught" << std::endl;
}

}

void myHandler() {
std::cout << "Unexpected exception caught" << std::endl;
throw std::exception();
}

-------- Produces the following Output -------------

terminate called after throwing an instance of 'std::string'
Aborted

-------- End -----------------

This is as expected. :)

The unexpected() handler is called when an exception, other that the
one(s) in the exception specification, leaves a function. It has
nothing to do with try-catch.

void function() throw(std::exception)
{
throw std::string("This would trigger it");
}


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Crossword 2
Throwing unexpected exceptions 5
unexpected exception 3
type 'int' unexpected 7
Unexpected behaviour 9
Unexpected termination 2
CIN Input #2 gets skipped, I don't understand why. 1
Crossword 14

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top