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 2 10350
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ) ...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
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...
| |