VC try catch all exceptions

Source: Internet
Author: User

Try {} catch (...) {} Used to use try {} catch (...) {} To catch some unexpected exceptions in C ++. Today I read winhack's post to know that this method is actually unreliable in VC. For example Code : Try {byte * PCH; PCH = (byte *) 00001234; // give an Invalid Address * PCH = 6; // assign a value to the Invalid Address, will cause access violation exception} catch (...) {afxmessagebox ("catched");} the code is normal in debug. The exception will be caught and a message box "catched" will pop up. However, if the compiler code optimization option is selected in the release mode, the VC compiler searches for the code in the try block. If the throw code is not found, he will think that the try catch structure is redundant and optimized. In the release mode, exceptions in the above Code cannot be captured, thus forcing Program The pop-up error prompt box exits. So can we catch this exception in the release code optimization status? The answer is yes. It is the _ Try, _ struct T structure. If the above Code is changed to the following code exception, it can be captured. _ Try {byte * PCH; PCH = (byte *) 00001234; // give an Invalid Address * PCH = 6; // assign a value to the Invalid Address, the access violation exception will occur.} _ response T (exception_execute_handler) {afxmessagebox ("catched");} but there is still a problem using the _ Try and _ Wait t blocks, this is not a C ++ standard, but a Windows platform-specific extension. In addition, if you call a local object destructor during use, the c2712 compilation error may occur. So there is no 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 optimize the try catch module. This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/startexcel/archive/2009/05/19/4201585.aspx

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.