C + + Exception capture

Source: Internet
Author: User

concept: "C+ + Exception "isTry{}Catch(...) {} "Seh exception" is __try{} __except (-1/0/1{} (for these two exceptions, if you do not know the place, there is a lot of information on the Web to refer to) the current Microsoft all VC compiler (from VC6 to VC2010), the default is to open the C+ + Exception compilation support (in Project options, code generation, enable C + + Exceptions:/EHSC,VC6 is enable Exception handling) (the following need to be read carefully) in VC6, the EXE project is also enabled by default "C+ + exceptions can catch Seh exceptions ". Under this condition, use the C + +exceptions "can also capture basic Windows exceptions such as read and write to illegal addresses. such as:Try{int*a=0; *a= -; }Catch(...) {printf ("excption!"), execute to *a=at 100, it jumps to the catch block and executes printf. However, VC6 built DLL or other non-EXE project, if it is release mode, and compile, enabled code automatic optimization, then the result becomes only enabled "C+ + exception ", or" C + +exception cannot catch Seh exception ". One consequence of this is that in DLLs, even if you use try{like thisint*a=0; *a= -; }Catch(...) {printf ("excption!");} Protect the code when the code executes to*A=100 an illegal address assignment statement,Catch(...) But nothing is caught, which can directly cause the program to crash. Unless you explicitly use the "SEH exception" __try...__except (1): To catch this kind of Windows exception. Corresponding solution: For starting from VC7 and after the VC+ +, in "code generation", "Enable C + + exceptions", there is a third option: "Valid, but with SEH exception (/EHa) "so that you can let the program" use C++An exception can also catch an SEH exception ". So whether it's in an EXE or a DLL,Try{}Catch(...) {} can also catch Windows exceptions. For VC6 non-EXE project, there are the following two methods to use try:Catch.. To capture the SEH exception for Windows:1, by turning off compile-time optimizations (disabling or only using the default level, personal feeling is because the VC6 compiler is not very well-developed). 2, by hand-adding/in the compilation options of the current projectThe EHa parameter. This compilation option is no interface can be set, can only be added manually. Both of these methods can be selected either. A second method is recommended. Also be aware that the "C++the exception "and" SEH exception "cannot be mixed in the same function at the same time. More0
It is best not to use C + + exceptions to catch SEH exceptions.

C + + Exception capture

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.