473,386 Members | 1,699 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,386 software developers and data experts.

Unexpected termination

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 with gcc 3.4.2-mingw.

I have tried the TCPL Spec.Ed. example of just adding std::bad_exception to
the exception specification of a function promising to throw an int, but
throwing a string-literal (effectively a pointer). I call that from main
inside a try bloc, with catching std::bad_exception after it. And yet, I
get terminate_handler being called, and gcc's verbose terminate handler
says:

"terminate called after throwing an instance of 'char const*'"

Now did I misunderstand Stroustrup? As I read the book if I add
std::bad_exception to the exception specification (and I also catch it) I
should not get terminate() called but I should have my char const* exception
be automagically translated into a std::bad_exception. Which does not
happen in my very simple code.

What did I miss? I am not waiting for gcc specific advice, I just would
like to know what the language should do. Although if someone knows for
sure that gcc (eh, g++) is faulty and let's me know, I won't be
disappointed. But I am mainly interested in the standard-mandated behavior.
If you could send a little code which works in your environment, I could
compare them...

I have the bad feeling I do something silly, but my code is so simple and I
see no place where it could be wrong. :-(

Do you see anything wrong? I must be something silly.

#include <iostream>
#include <exception>

void
liar_liar() throw(int,std::bad_exception) {
// Jim Carrey is not an int, but a character :-)
throw "Jim Carrey";
}

//============================================

int main(int argc, char const *argv[]) {

// Forget this, just gives debugging aid
std::set_terminate(__gnu_cxx::__verbose_terminate_ handler);

try {
liar_liar();
}
catch(char const *s) { ; }
catch(std::bad_exception &e) {
std::cout << "Bad exception caught: "
<< e.what() << std::endl;
}
}

--
Attila aka WW
Jul 22 '05 #1
2 10392
Attila Feher wrote:
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 with gcc 3.4.2-mingw.

I have tried the TCPL Spec.Ed. example of just adding std::bad_exception to
the exception specification of a function promising to throw an int, but
throwing a string-literal (effectively a pointer). I call that from main
inside a try bloc, with catching std::bad_exception after it. And yet, I
get terminate_handler being called, and gcc's verbose terminate handler
says:

"terminate called after throwing an instance of 'char const*'"

Now did I misunderstand Stroustrup? As I read the book if I add
std::bad_exception to the exception specification (and I also catch it) I
should not get terminate() called but I should have my char const* exception
be automagically translated into a std::bad_exception. Which does not
happen in my very simple code.

What did I miss? I am not waiting for gcc specific advice, I just would
like to know what the language should do. Although if someone knows for
sure that gcc (eh, g++) is faulty and let's me know, I won't be
disappointed. But I am mainly interested in the standard-mandated behavior.
If you could send a little code which works in your environment, I could
compare them...

I have the bad feeling I do something silly, but my code is so simple and I
see no place where it could be wrong. :-(

Do you see anything wrong? I must be something silly.
You need to use the 'unexpected' function handler here.

From the C++ specification -

Section 15.5.2
2) ... * If the exception-specification does not include the class
std::bad_exception (18.6.2.1) then the function terminate() is called,
otherwise the thrown exception is replaced by an implementation-defined
object of the type std::bad_exception and the search for another handler
will continue at the call of the function whose exception-specification
was violated **

3) Thus, an exception-specification guarantees that only the listed
exceptions will be thrown. If the exception-specification includes the
type std::bad_exception then any exception not on the list may be
replaced by std::bad_exception within the function unexpected().

Add an unexpected_handler as mentioned inline.
It should work fine.

There is no use in dealing with terminate_handler for these cases
since it is not supposed to return anyway.


#include <iostream>
#include <exception>

void
liar_liar() throw(int,std::bad_exception) {
// Jim Carrey is not an int, but a character :-)
throw "Jim Carrey";
}
void unexpected_handler() {
throw 1.2;
}

//============================================

int main(int argc, char const *argv[]) {

// Forget this, just gives debugging aid
std::set_terminate(__gnu_cxx::__verbose_terminate_ handler);
std::set_unexpected( unexpected_handler);

try {
liar_liar();
}
catch(char const *s) { ; }
catch(std::bad_exception &e) {
std::cout << "Bad exception caught: "
<< e.what() << std::endl;
}
}

--
Attila aka WW


--
Karthik.
Jul 22 '05 #2
Karthik Kumar wrote:
Attila Feher wrote: [SNIP]
Do you see anything wrong? I must be something silly.


You need to use the 'unexpected' function handler here.

From the C++ specification -

Section 15.5.2

[SNIP] Add an unexpected_handler as mentioned inline.
It should work fine.
Yeah. The Stroustrup book is pretty vague on describing this. Reading the
standard makes it clear that one needs to define and set a custom unexpected
handler, but reading he text of The C++ Programming language at 14.6.3 gives
the impression that it is enough to add std::bad_exception.

Thanks for the section number of the standard. I still have difficulties
orienteering in those parts of it which I do not use regurarly.
There is no use in dealing with terminate_handler for these cases
since it is not supposed to return anyway.


Except (as it is mentioned clearly in the posted code) that the given
terminate handler *tells* why the terminate was called, do it had a very
important use for me.

--
Attila aka WW
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alexander Staubo | last post by:
Python does not seem to clean up gracefully on SIGTERM: The exit sequence is not invoked on termination, so the atexit module cannot be used to install shutdown logic. Further, while the signal...
2
by: Jim McGrail | last post by:
Background: I am investigating a problem involving a windows .NET application that is being developed in C# with Visual Studio 2003. It is a multi-threaded application that uses MSMQ to...
1
by: Najm Hashmi | last post by:
Hi all , I am trying to create a store procedure and I get the following error: SQL0104N An unexpected token "END-OF-STATEMENT" was found following "END". Expected tokens may include: "JOIN...
35
by: Felix Kater | last post by:
The C-faq says that "The malloc/free implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing." Could that length...
2
by: José Joye | last post by:
I have a library written in C (I do not have the source) and having some callbacks exported. It is currently not that stable and write to stdout and stderr :-(( The idea I have is to wrap it with...
32
by: Rene Pijlman | last post by:
One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
0
by: vsankar9 | last post by:
It's an Service Contracts From CRM. I need termination amount for who's the customer is terminated . End date - 15-DEC-2007 Termination as of date - 15-MAR-2007 Rate - 1000 (monthly rate ) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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...

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.