473,388 Members | 1,557 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,388 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 3436

"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!...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.