By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

unexpected behaviour of set_unexpected()

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

"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

P: n/a
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.