Use exception methods and enable them in Visual Studio

Source: Internet
Author: User
Tags try catch

When using WindowsProgramI believe no users like to see the sudden program crash! To avoid program crashes, it is best for programmers to use an exception processor to ensure a friendly user experience when writing programs that are prone to errors. Especially C/C ++CodeCrash is a common occurrence!
Today, a colleague told me that writing C/C ++ code crashes mainly because of Memory Operation violations. If you check whether the memory or pointer is valid before each memory or pointer operation, you can reduce the number of program crashes. But this will make programmers very bored, haha. So adding exception handling in the right place will make the programmer better improve the program even if the crash occurs. Of course, the program efficiency will inevitably decrease!

Fortunately, the C ++ specification has an exception handling mechanism:Try catch

However, using try catch directly in Visual Studio does not produce exceptions. You must manually throw the exception throw. See the following code:

 Void Trycatchfourth ()
{
Char * Test = NULL;
Try
{
Test = New Char [ 2 ];
Freearray (test );
Throw 0 ; // Here I throw an exception to test
// Without manual throw, the code in the catch will not be executed even if an exception occurs.
Puts ( " No " ); // The following two statements will not be executed
* (Test + 4096 ) = ' \ 0 ' ;
}
Catch (...)
{
If (Null! = Test)
{
Freearray (test );
}
Printf ( " 4 \ n " );
}
Puts ( " Fourth " );
}

The following code modifies the vs compiler options: Open project properties → configure properties → C/C ++ → code generation → enable C ++ exceptions → yes, but there is a seh exception (/EHA) (here the default compiler is "Yes (/ehscc )").

 Void Trycatchthree ()
{
Char * Test = NULL;
Try
{
Test = New Char [ 2 ];
Freearray (test );
* (Test + 4096 ) = ' \ 0 ' ;
}
Catch (...)
{
If (Null! = Test)
{
Freearray (test );
}
Printf ( " 4 \ n " );
}
Puts ( " Three " );
}

After modifying the compiler options, run trycatchthree.Note the different compilation options used by trycatchthree () and trycatchfourth.

Finally, refer to a Windows structured exception handling code:

 Void Trycatchsecond ()
{
Char * Test = NULL;
_ Try
{
_ Try
{
Test = New Char [ 2 ];
Freearray (test );
* (Test + 4096 ) = ' \ 0 ' ;
// The number here must be large; otherwise, no exception occurs.
// In Windows, the system allocates n Bytes larger than the n Bytes.
// The size of 4096 is exactly the size of a page. If n> 4096, expand the corresponding number to generate an exception.
}
_ Finally
{
If (Null! = Test)
{
Freearray (test );
}
Printf ( " 2 " ); // This is printed second
}
}
_ Effect (filterfunction (getexceptioncode ()))
{
Printf ( " 3 \ n " ); // This is printed last
}
Puts ( " Second " );
}

Trycatchsecond () works normally regardless of whether the compiler option is modified as required. My colleague said that this is a Windows structured exception. It is a little different from the exception in C ++. I don't know much about it now. I will find the information and add it to my blog later. The trycatchsecond () code is an example in msdn, as follows:

DWORD filterfunction (Int I = 1 )
{
Printf ( " % D " , I ); // Printed first
Return Prediction_execute_handler;
}

Void Trycatchfirst ()
{
Char * Test = NULL;
_ Try
{
_ Try
{

// This API sets the exception code manually (this is a bit awkward)
Raiseexception ( 1 , // Exception Code
0 , // Continuable exception
0 , Null ); // No arguments
}
_ Finally
{
Printf ( " 2 " ); // This is printed second
}
}
_ Limit T (filterfunction ())
{
Printf ( " 3 \ n " ); // This is printed last
}
Puts ( " One " );
}

The complete code is as follows :(The code in this article is written and tested in vs2008. We recommend that you do not test the code in vc6. vc6 has poor support for C ++ specifications.)

View code

# Include <windows. h>

// Release Array Memory
# Define Freearray (parray ){\
Delete [] parray ;\
Parray = NULL ;\
}
DWORD filterfunction ( Int I = 1 )
{
Printf ( " % D " , I ); // Printed first
Return Prediction_execute_handler;
}

Void Trycatchfirst ()
{
Char * Test = NULL;
_ Try
{
_ Try
{

// This API sets the exception code manually (this is a bit awkward)
Raiseexception ( 1 ,// Exception Code
0 , // Continuable exception
0 , Null ); // No arguments
}
_ Finally
{
Printf ( " 2 " ); // This is printed second
}
}
_ Limit T (filterfunction ())
{
Printf ( " 3 \ n " ); // This is printed last
}
Puts ( " One " );
}

Void Trycatchsecond ()
{
Char * Test = NULL;
_ Try
{
_ Try
{
Test = New Char [ 2 ];
Freearray (test );
* (Test +4096 ) = ' \ 0 ' ;
// The number here must be large; otherwise, no exception occurs.
// In Windows, the system allocates n Bytes larger than the n Bytes.
// The size of 4096 is exactly the size of a page. If n> 4096, expand the corresponding number to generate an exception.
}
_ Finally
{
If (Null! = Test)
{
Freearray (test );
}
Printf ( " 2 " ); // This is printed second
}
}
_ Effect (filterfunction (getexceptioncode ()))
{
Printf ( " 3 \ n " );// This is printed last
}
Puts ( " Second " );
}
Void Trycatchthree ()
{
Char * Test = NULL;
Try
{
Test = New Char [2 ];
Freearray (test );
* (Test + 4096 ) = ' \ 0 ' ;
}
Catch (...)
{
If (Null! = Test)
{
Freearray (test );
}
Printf ( " 4 \ n " );
}
Puts ( " Three " );
}
Void Trycatchfourth ()
{
Char * Test = NULL;
Try
{
Test = New Char [ 2 ];
Freearray (test );
Throw 0 ; // Here I throw an exception to test
// Without manual throw, the code in the catch will not be executed even if an exception occurs.
Puts ( " No " ); // The following two statements will not be executed
* (Test +4096 ) = ' \ 0 ' ;
}
Catch (...)
{
If (Null! = Test)
{
Freearray (test );
}
Printf ( " 4 \ n " );
}
Puts (" Fourth " );
}

Void main (void)
{
// Trycatchfirst ();
// Trycatchsecond ();
// Trycatchthree ();
// Trycatchfourth ();
}

 

[Thank you for your reference]
1. My colleagues

 

Related Article

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.