Document directory
- Let's take a simple example.
- To a true C ++ program, and use the seh Mechanism
- Summary
Reprint from http://blog.programfan.com/article.asp? Id = 9864
First, we declare that the exception handling mechanism in C ++ is built on the seh mechanism on Windows, so seh can certainly be used in C ++ programs. However, "A Yu" has repeatedly stressed that we generally use try-catch t and try-finally exception mechanisms in a narrow sense, and they are used for C language (VC environment) designed for writing Windows Driver, seh should be used in C Programs, the C ++ program should use the try-catch mechanism's c ++ Exception Handling Model (the msdn of micorsoft strongly recommends that programmers follow this rule ). However, can seh be used in c ++ programs? "Of course we can, certainly." In fact, this point was clearly stated at the beginning when we explained the relationship between various exception mechanisms on the Windows platform.
This article systematically looks at the various usage of seh in the C ++ program.
Let's take a simple example.
In fact, a simple example is to rename the C program using the seh mechanism as a C ++ program to see if it can be compiled and run normally? What does it mean? It's very simple. change the C program extension. CPP file, that is, the VC compiler uses the C ++ language to compile the program (this is the c ++ program ). Try it, friends! The Code is as follows:
// Note that this is the c ++ program, file name: SEH-test.cpp
# Include "stdio. H"
Void test ()
{
Int * p = 0x00000000; // pointer to null
_ Try
{
Puts ("in try ");
_ Try
{
Puts ("in try ");
// Causes an access violation exception;
// Cause a storage exception
* P = 13;
Puts ("won't be executed here ");
}
_ Finally
{
Puts ("in finally ");
}
Puts ("This will not be executed here ");
}
_ Partition T (puts ("in filter 1"), 0)
{
Puts ("in bytes T 1 ");
}
}
Void main ()
{
Puts ("hello ");
_ Try
{
Test ();
}
_ Partition T (puts ("in filter 2"), 1)
{
Puts ("in bytes T 2 ");
}
Puts ("world ");
}
Can it be compiled, and the running result is exactly the same as that of the previous C program, as shown below:
Hello
In try
In try
In filter 1
In filter 2
In finally
In bytes T 2
World
Press any key to continue
To a true C ++ program, and use the seh Mechanism
Many programmers may disagree with the above example and feel that it cannot explain any problem. Okay. Now let's look at a real C ++ program and use the seh mechanism. What is a true C ++ program, of course, adopts the object-oriented technology. Let's look at the example! The Code is as follows (in fact, a class is added on the basis of the above program ):
// Note that this is the c ++ program, file name: SEH-test.cpp
# Include "stdio. H"
Class
{
Public:
Void F1 (){}
Void F2 (){}
};
Void test1 ()
{
A A1;
A A2, A3;
A2.f1 ();
A3.f2 ();
}
Void test ()
{
Int * p = 0x00000000; // pointer to null
_ Try
{
// Call the test1 function. The function creates an object.
// It should belong to the 100% C ++ program!
Test1 ();
Puts ("in try ");
_ Try
{
Puts ("in try ");
// Causes an access violation exception;
// Cause a storage exception
* P = 13;
Puts ("won't be executed here ");
}
_ Finally
{
Puts ("in finally ");
}
Puts ("This will not be executed here ");
}
_ Partition T (puts ("in filter 1"), 0)
{
Puts ("in bytes T 1 ");
}
}
Void main ()
{
Puts ("hello ");
_ Try
{
Test ();
}
_ Partition T (puts ("in filter 2"), 1)
{
Puts ("in bytes T 2 ");
}
Puts ("world ");
}
Summary
According to the above practices, seh can indeed be used in C ++ programs, and seh can be used not only in C ++ programs; further, the seh exception mechanism (try-catch t and try-finally) can also be used together with the try-catch model in the same C ++ program, this will be discussed in the next article.
But the problem is that Microsoft's msdn advice (try-catch and try-finally are used in C Programs, while try-catch should be used in C ++ programs ), isn't that a threat? None! None! Microsoft's suggestion is not wrong at all. When seh is used in combination with the c ++ Exception Handling Mechanism, there are indeed some constraints (although we seldom care about this at ordinary times ), this will also be discussed in detail in the next article, programmers and friends. Continue!