-
November 29th, 2010, 09:51 AM
#1
Why the output?
Code:
#include <iostream>
#include <exception>
using namespace std;
void myunexpected () {
cerr << "unexpected called\n";
throw 0; // throws int (in exception-specification)
}
void myfunction () throw (int) {
throw 'x'; // throws char (not in exception-specification)
}
int main (void) {
set_unexpected (myunexpected);
try {
myfunction();
}
catch (int) { cerr << "caught int\n"; }
return 0;
}
Expected output:
unexpected called
caught int
Actual Output: No output is displayed and the program aborts.
-
November 29th, 2010, 10:07 AM
#2
Re: Why the output?
With GCC/mingw, I get your expected output.
Although to be honest, NOBODY uses exception specifiers, except maybe for the rare function/object that guarantees to throw nothing. Also, your aren't supposed to return from unexpected (unexpected is supposed to terminate the program), so you shouldn't throw from unexpected either.
I can't tell you why you are not getting the "expected" behaviour, but I will tell you that you are going into real deep and obscure C++ semantics, and using them in ways they were not meant to be used. The resulting mix is that a definite answer will be very hard to give.
What's your compiler? What are your options?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 29th, 2010, 10:14 AM
#3
-
November 29th, 2010, 10:36 AM
#4
Re: Why the output?
When compiling with VStudio, you get warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow). What this basically means is that the microsoft compiler hasn't implemented exception specifications (except no throw), and will be quite happy with throwing your char. What happens next though is that you do not catch that char, and then you terminate, because of an un-caught exception.
try this:
Code:
#include <iostream>
#include <exception>
void myunexpected () {
std::cerr << "unexpected called\n";
throw 0; // throws int (in exception-specification)
}
void myfunction () throw (int) {
throw 'x'; // throws char (not in exception-specification)
}
int main (void) {
std::set_unexpected (myunexpected);
try {
myfunction();
}
catch (int) { std::cerr << "caught int\n"; }
catch (char) { std::cerr << "caught char\n"; }
return 0;
}
Somebody with more VStudio experience can tell you how to force exception specifications, but I don't think it is even possible.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 30th, 2010, 07:30 AM
#5
Re: Why the output?
Visual Studio does not support exception specifications up to now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|