Sign in to follow this  

unexpected exception

This topic is 4586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all according to "Think in C++ Volume2", the code below should run smoothly
#include <iostream>
#include <exception>
using namespace std;

class ex {
};

void func() throw(ex) {
	throw 10;
}

void my_unexpected() {
	cout << "unexpected exception" << endl;  // this never shows up on my VC compilers
	exit(0);
}

int main()
{
	set_unexpected(my_unexpected);

	try {
		func();
	}
	catch(ex) {
		cout << "ex caught" << endl;
	}

	return 0;
}

But on my VC2003 and VC2005 BETA2, there will be a compiling warning and a runtime error VC doesn't support unexpected exception ? or something else ?

Share this post


Link to post
Share on other sites
Hi, what is the compiler warning?

You might like to check in the project properties (VS2k3: C/C++ -> Code Generation) to see whether exceptions are enabled.

Share this post


Link to post
Share on other sites
the warning message is:
warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

and exceptions are surely enabled in the project properties

Share this post


Link to post
Share on other sites
It means precisely what it says. Exception specifications, beyond indicating that a function never throws (or propagate) an exception, are ignored by VC++ (and a number of other compilers). Which means that trying to set an unexpected exception handler is futile. Similarly, there is a function (whose name evades me) that is supposed to tell you whether an exception is already pending (so that you don't throw an exception from within an exception handler), which generally isn't implemented either. And I believe there's yet another feature in that category that generally doesn't get implemented, but I can't remember what it is or if it even exists (it might just be the pending exception functions and I got confused).

Share this post


Link to post
Share on other sites
Well, this is just a guess, but I'd say it's because in your func() definition you specified that it would throw a type of ex -- instead, you threw an integer value. Maybe try:

#include <iostream>

class ex { }; //empty exception class
void func() throw(ex); //throw(ex) means it throws a type of ex

void func() throw(ex) {
if(1>2) throw ex; //someone check this, not sure if
//this is valid
}

int main() {
try {
func()
}
catch(ex) {
std::cout << "ex caught!\n";
}
catch(...) {
std::cout << "Unexpected exception caught!\n";
}
return 0;
}


...But don't quote me on that. Exceptions are an iffy spot for me. If anybody's got corrections, say something, because I'd like to learn a little more about them, too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boku San
Well, this is just a guess, but I'd say it's because in your func() definition you specified that it would throw a type of ex -- instead, you threw an integer value.


An exception specification is a list of types a given function may throw. In Java, the compiler statically checks them: if your function calls other functions that throw exceptions not listed in its specification, then it must catch them, or your program does not compile. C++ doesn't do that, which makes its exception specification mechanism rather useless. Instead, if your function happens to throw an exception that isn't listed, the program calls std::unexpected() instead, or a user function specified through std::set_unexpected(). In practice, the only exception specification that is widely supported is the no-throw throw() specification, which informs the compiler that the function is guaranteed not to throw (which may have performance consequences).

The whole point of neverland's code was to throw an exception that wasn't listed (int instead of ex) to trigger the call to my_unexpected(). He found out that VC doesn't support that feature: all std::unexpected() are routed to std::terminate().

There, I remember the third feature I had alluded to in my previous post: std::set_terminate().

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Similarly, there is a function (whose name evades me) that is supposed to tell you whether an exception is already pending (so that you don't throw an exception from within an exception handler), which generally isn't implemented either.


Do you mean in the process of being thrown (e.g. between throw and catch) or actually within the catch? If the former, see uncaught_exception. If the later, I've never heard of it, nor does it seem to be in the MSDN's documentation of the <exception> header.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Do you mean in the process of being thrown (e.g. between throw and catch) or actually within the catch? If the former, see uncaught_exception.


Yeah, that's what I meant. I was working from memory. Thanks. [smile]

Share this post


Link to post
Share on other sites

This topic is 4586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this