Try and catch problems in VC

Source: Internet
Author: User

 

Try {} catch (...){}

In the past, try {} catch (...) was used (...) {} to catch some unexpected exceptions in C ++. Today, I read winhack's post and I realized that this method is actually used in VC.

It is unreliable. For example, the following code;

 

Try <br/> {<br/> byte * PCH; <br/> PCH = (byte *) 00001234; // give an invalid address <br/> * PCH = 6; // assign a value to the Invalid Address, access violation exception <br/>}< br/> catch (...) <br/>{< br/> afxmessagebox ("catched"); <br/>}

There is no problem with this code in debug. Exceptions will be caught and a message box of "catched" will pop up. However, if the compiler code is selected in the release mode

Optimization Options, the VC compiler will search for the code in the try block. If the throw code is not found, the VC compiler will think that the try catch structure is redundant and optimized.

In release mode, exceptions in the above Code cannot be captured, and the program is forced to exit with an error prompt box.

 

So can we catch this exception in the release code optimization status? The answer is yes. Is the structure of _ Try, _ struct T. If the above Code is changed to the following code:

Exception can be captured

_ Try <br/>{< br/> byte * PCH; <br/> PCH = (byte *) 00001234; // give an invalid address </P> <p> * PCH = 6; // assign a value to the Invalid Address, access violation exception may occur <br/>}< br/>__ except T (exception_execute_handler) <br/>{< br/> afxmessagebox ("catched "); <br/>}< br/> 

But there is still a problem when using the _ Try, __except block, that is, this is not a C ++ standard, but a Windows platform-specific extension. In addition

When you design a local object destructor call, a c2712 compilation error occurs. Is there any other way?
Of course, it is still using the C ++ standard try {} catch (...) {}, but adding the/EHA parameter to the compile command line. In this way, the VC compiler will not take the try and catch modules

Optimized

 

From: http://www.doyj.com/2006/09/11/try-catch/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.