try/catch don't work under release version???
I use try/catch a lot in my project. They work fine under debug version but it seems the exceptions are not caught under release version. I am using VS2010. Thanks.
Re: try/catch don't work under release version???
Thanks for the info. :rolleyes:
Re: try/catch don't work under release version???
Quote:
Originally Posted by
VictorN
Thanks for the info. :rolleyes:
This is not for the info purpose only and I am also looking for the answer. :-)
Re: try/catch don't work under release version???
Quote:
Originally Posted by
LarryChen
This is not for the info purpose only and I am also looking for the answer. :-)
But you didn't ask a question!
Re: try/catch don't work under release version???
Quote:
Originally Posted by
VictorN
But you didn't ask a question!
Okey, Actually my project works fine under debug version but exceptions are not handled under release version. My question is that how am I able to handle the exceptions under release version? Thanks.
Re: try/catch don't work under release version???
Make sure exceptions are enabled in your project settings.
Re: try/catch don't work under release version???
Quote:
Originally Posted by
D_Drmmr
Make sure exceptions are enabled in your project settings.
I already checked "Enable C++ exceptions" with option /EHa. What else did I miss? Thanks.
Re: try/catch don't work under release version???
Quote:
Originally Posted by
LarryChen
I already checked "Enable C++ exceptions" with option /EHa. What else did I miss? Thanks.
How do you know that exceptions are not enabled. Did you throw an exception?
Regards,
Paul McKenzie
Re: try/catch don't work under release version???
You could try fixing whatever is wrong.
Re: try/catch don't work under release version???
Quote:
Originally Posted by
Paul McKenzie
How do you know that exceptions are not enabled. Did you throw an exception?
Regards,
Paul McKenzie
Like I said if I try to run the project under debug mode, then exceptions will be caught. The exceptions are not caught under release mode under same circumstance.
Re: try/catch don't work under release version???
Quote:
Originally Posted by
LarryChen
Like I said if I try to run the project under debug mode, then exceptions will be caught. The exceptions are not caught under release mode under same circumstance.
What exceptions? You didn't post any code. How do we know exceptions are actually being thrown in release mode?
Nothing to catch if there is nothing thrown.
In any case, nobody can help you unless you actually give them something to look at.
Re: try/catch don't work under release version???
Quote:
Originally Posted by
LarryChen
Like I said if I try to run the project under debug mode, then exceptions will be caught. The exceptions are not caught under release mode under same circumstance.
Again, how do you know there are exceptions to be caught? If nothing is thrown, nothing gets caught. So where is your evidence that an exception was thrown and you didn't catch it?
A debug build (one where the _DEBUG preprocessor is set), produces a program that does all sorts of checks that do not exist in release builds. Add to that, a release build is optimized, meaning the code you see in the debugger doesn't match what is actually being executed. The code you claim throws an exception could have been optimized away. So there is no "same circumstance" as a debug build.
Also, what types of exceptions? Memory exceptions? Then you are hoping that the same buggyness in your debug build will exist in your release build to cause an exception to occur?
Regards,
Paul McKenzie
Re: try/catch don't work under release version???
Quote:
Originally Posted by
Paul McKenzie
Again, how do you know there are exceptions to be caught? If nothing is thrown, nothing gets caught. So where is your evidence that an exception was thrown and you didn't catch it?
A debug build (one where the _DEBUG preprocessor is set), produces a program that does all sorts of checks that do not exist in release builds. Add to that, a release build is optimized, meaning the code you see in the debugger doesn't match what is actually being executed. The code you claim throws an exception could have been optimized away. So there is no "same circumstance" as a debug build.
Also, what types of exceptions? Memory exceptions? Then you are hoping that the same buggyness in your debug build will exist in your release build to cause an exception to occur?
Regards,
Paul McKenzie
There is an access violation exception so that it causes the program crash. So that is why I am sure an exception is thrown but it is not handled under release version. In the source code I do have a handler for such exception but obviously the handler is never called under release version. If as you claimed, a release build is optimized, then I might expect an exception is thrown under debug version but not under release version, which is exactly opposite to what is happening to my project.
Re: try/catch don't work under release version???
Quote:
Originally Posted by
LarryChen
There is an access violation exception so that it causes the program crash. So that is why I am sure an exception is thrown but it is not handled under release version. In the source code I do have a handler for such exception but obviously the handler is never called under release version. If as you claimed, a release build is optimized, then I might expect an exception is thrown under debug version but not under release version, which is exactly opposite to what is happening to my project.
LarryChen, how do you know you have a access violation being thrown?
The point is, you don't. You haven't said anything to make me think you do anyway.
Debug and release builds will be vastly different, and not every out of bounds memory access will produce an exception.
Try this in both release and debug mode. I have a feeling you will get the exception in release mode that you were expecting.
Code:
*(int*)0 = 0; //This will produce a memory access violation
Edit: Just for clarification. Your code should look like this:
Code:
__try
{
*(int*)0 = 0;
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
{
cout << "Access Violation!" << endl;
}
Works for me, and it should work for you too. Also, keep in mind that this is completely MSVC/Windows specific. This will not work on any other OS or compiler (maybe some other Windows compiler.)
Re: try/catch don't work under release version???
Sorry to revive a dead thread, but I had this same problem. Try Catch works under debug but not release for x64 compiler using VS2010. The issues seems to be that Try Catch aren't implemented if the compiler is set to optimize the code. Right click project->configuration properties->c/c++->Optimization->disabled under the Release Version should re-enable Try Catch stuff.