locked
catch(...) behavior are different in Debug and Release mode ? RRS feed

  • Question

  • Hi,
        I have wrote an simple program to test catch(...) statement using VS 2003.
        But I found that in the Debug mode, it can catch the error; but in the Release mode, it failed ...
        How can I fix the problem?
        Thanks A Lot,
        
       
    #include <iostream>
    using namespace std;

    int main()
    {
     int* p = 0;

     try
     {
      *p = 13; // causes an access violation exception;
     }
     catch(...)
     {
      cout << " in catch(…) block ..." << endl;
     }

     return 0;
    }

    Friday, October 13, 2006 6:52 AM

Answers

  • The difference isn't DEBUG vs RELEASE, but which exception model you are using. Switch to release build and check the Project > [projectname] Properties > Configuration Properties > C/C++ > Code Generation > Enable C++ Exception setting. Switch this to /EHa, and your application should behave as you please.
    Friday, October 13, 2006 7:28 AM

All replies

  • The difference isn't DEBUG vs RELEASE, but which exception model you are using. Switch to release build and check the Project > [projectname] Properties > Configuration Properties > C/C++ > Code Generation > Enable C++ Exception setting. Switch this to /EHa, and your application should behave as you please.
    Friday, October 13, 2006 7:28 AM
  • Hi, Einaros:
        Thanks A Lot for your help.
        I've tried again by your description, and I found that the C/C++ > Code Generation > Enable C++ Exception option is a combobox, so I have to choose No or Yes, but if I choose Yes, the compile option will be /EHsc, not /EHa ... (maybe I use is Chinese Traditional version VS 2003, so that it is somewhat different with your environment)
        So, I set the switch /EHa in C/C++ > Code Generation > Command Line, it can work ^^.
        Thanks again,
        Best Regards,
                     Jasper 10/14

     

    Saturday, October 14, 2006 5:22 AM