468,771 Members | 1,490 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

unexpected behaviour of set_unexpected()

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 -----------------

Apr 14 '06 #1
2 3122

"bb" <mu**********@gmail.com> skrev i meddelandet
news:11*********************@g10g2000cwb.googlegro ups.com...
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
Apr 14 '06 #2
bb
Thanks Bo Persson. I realized after playing around.

Apr 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Attila Feher | last post: by
3 posts views Thread by Teddy | last post: by
3 posts views Thread by Michał ¦liwka | last post: by
2 posts views Thread by Gerhard Esterhuizen | last post: by
10 posts views Thread by sindica | last post: by
9 posts views Thread by Jeff Louie | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.