473,224 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 3425

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Attila Feher | last post by:
Hi all, I have not done much work around exceptions; and even when I do I avoid exception specifications. But now I have to teach people about these language facilities, so I am trying them out...
3
by: Teddy | last post by:
Hello all According to "Think in C++ Volume2", the code below should run smoothly: #include <iostream> #include <exception> using namespace std; class ex { };
3
by: Michał ¦liwka | last post by:
Hi, I want to use set_terminate and set_unexpected to control errors in my program, but it seems that theese two functions don't do anything whet MFC is in use. Have you got any idea? I...
2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
10
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
23
by: gu | last post by:
hi to all! after two days debugging my code, i've come to the point that the problem was caused by an unexpected behaviour of python. or by lack of some information about the program, of course!...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.