A
Attila Feher
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;
}
}
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;
}
}