Sign in to follow this  
d000hg

Non-MFC exception handling

Recommended Posts

I've found a point where an exception is generated in my application on a few people's machines. It seems to be occurring as a function returns rather than in any code which is executed which is odd in itself - any thoughts on that? Because this is a non-MFC project, CException isn't available to me I think. So other than catch(...), what can I do to find what the exception is? clarification: If I wrap the offending function call in a try block, followed by catch(...), this catch block is triggered. However this gives me no information about the exception, just that it happened! Thankyou [Edited by - d000hg on February 8, 2005 7:09:39 AM]

Share this post


Link to post
Share on other sites
Another possibility would be to use the Windows SEH (structured exception handling). It may be a VC++ thingy, and I don't know if it works with gcc or any other compiler.

(wait: there is something weird with this page... try here).

HTH,

Share this post


Link to post
Share on other sites
Well I want to use try & catch, but Imust be able to do catch(something) that will catch the exception as CException does? MFC is just using plain c++...
If I wasn't using MFC can I detect an access violation etc using catch(something)?

Share this post


Link to post
Share on other sites
You can still use try/catch but they will only catch C++ exceptions (those which are throwed). SEH can catch access violations and a lot of other errors (like divide by 0 and so on).

Reading your post, it seems you are not very clear with C++ exceptions. You don't have to use MFC to be able to use them. You don't have to define an exception object to use them. This (pseudo) code works:

try {
if (cond1) throw 1000;
if (cond2) throw "a string";
if (cond3) throw new ComplexObject(1000, "a string");
}
catch (int e) {
}
catch (const char *c) {
}
catch (ComplexObject *obj) {
}

Regards,

Share this post


Link to post
Share on other sites
I'm quite happy with c++ exception handling, but if I do

int *pointer=0;
try{
*pointer=12345;
}
catch(...)
{
}
I believe the catch block will get called. In MFC I could do

int *pointer=0;
try{
*pointer=12345;
}
catch(CException *E)
{
}
And I think I could then get a string telling me it was an access violation. My point is that CException is an MFC class, so while I can trap such errors without crashing the program, I don't know what has gone wrong. I basically want to know how CException works and how to duplicate it - it tells you an exception code like 0xc0000005 and a string like "Access violation"...

Share this post


Link to post
Share on other sites
I think you want to look at _set_se_translator(). Be sure to compile with /EHa (asynchronous exception support) or you'll be in a world of hurt.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

try
{
.....
}
catch(exception &e)
{
cout << e.what();
}

Share this post


Link to post
Share on other sites
That seems very useful - thanks.
Although:
try
{
int *p=0;
*p=12345;
}
catch(CException *E)
{
...
}
catch(exception &e)
{
...
}
catch(...)
{
...
}
In this instance the last catch block is triggered, but I thought CException would catch it?

Share this post


Link to post
Share on other sites

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