CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: try/catch don't work under release version???

    Thanks for the info.
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Smile Re: try/catch don't work under release version???

    Quote Originally Posted by VictorN View Post
    Thanks for the info.
    This is not for the info purpose only and I am also looking for the answer. :-)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: try/catch don't work under release version???

    Quote Originally Posted by LarryChen View Post
    This is not for the info purpose only and I am also looking for the answer. :-)
    But you didn't ask a question!
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: try/catch don't work under release version???

    Quote Originally Posted by VictorN View Post
    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.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: try/catch don't work under release version???

    Make sure exceptions are enabled in your project settings.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jul 2005
    Posts
    1,030

    Re: try/catch don't work under release version???

    Quote Originally Posted by D_Drmmr View Post
    Make sure exceptions are enabled in your project settings.
    I already checked "Enable C++ exceptions" with option /EHa. What else did I miss? Thanks.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: try/catch don't work under release version???

    Quote Originally Posted by LarryChen View Post
    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

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: try/catch don't work under release version???

    You could try fixing whatever is wrong.

  10. #10
    Join Date
    Jul 2005
    Posts
    1,030

    Re: try/catch don't work under release version???

    Quote Originally Posted by Paul McKenzie View Post
    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.

  11. #11
    Join Date
    Aug 2008
    Posts
    902

    Re: try/catch don't work under release version???

    Quote Originally Posted by LarryChen View Post
    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.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: try/catch don't work under release version???

    Quote Originally Posted by LarryChen View Post
    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
    Last edited by Paul McKenzie; January 7th, 2011 at 10:10 PM.

  13. #13
    Join Date
    Jul 2005
    Posts
    1,030

    Re: try/catch don't work under release version???

    Quote Originally Posted by Paul McKenzie View Post
    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.

  14. #14
    Join Date
    Aug 2008
    Posts
    902

    Re: try/catch don't work under release version???

    Quote Originally Posted by LarryChen View Post
    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.)
    Last edited by Chris_F; January 8th, 2011 at 11:11 PM.

  15. #15
    Join Date
    Feb 2013
    Posts
    5

    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.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured