Exception Handling mechanisms provided by Windows operating systems

Source: Internet
Author: User
Tags throw exception

Reproduced from-http://blog.programfan.com/article.asp? Id = 9836

As you know, C ++ has a sound Exception Handling Mechanism, which is also supported by the C language. In addition, in many other modern programming languages, they also have their own exception handling programming mechanisms, such as the Ada Language. So why is the exception handling mechanism provided by the operating system platform still discussed here? This is because in many systems, the implementation of the exception handling mechanism provided by programming languages is based on the exception handling mechanism provided by the operating system, for example, the C ++ Exception Handling Model Implemented by the VC compiler on Windows is built on the seh mechanism. In the "secret of Love" article, when discussing the implementation of the Exception Handling Model in VC, we will conduct more in-depth research and analysis. In addition, of course, there are other systems, such as the Linux operating system GCC does not use the exception handling mechanism provided in the operating system. However, there is a major drawback, that is, for application developers, it cannot be very good in their own applications, to effectively control unexpected system exceptions in the operating system, such as possible segment errors during program execution and computing exceptions such as Division by 0, and many other types of system exceptions. Therefore, in GCC-compiled programs on Linux operating systems, it can only capture exceptions that have been explicitly throw by itself, but for system exceptions, catch Block is useless.

Therefore, the exception handling mechanism provided in the operating system platform is very necessary. Moreover, the implementation of the exception handling mechanism is also an important topic in the design of the operating system. Generally, the exception handling mechanism provided by Unix-like operating systems is very familiar to everyone, that is, the signal processing (Signal Handling) in the operating system ), it seems that this should also be the exception handling interface defined by the POSIX standard. Therefore, the Windows operating system platform also supports this mechanism. The "semaphore processing" programming mechanism will be further discussed in later sections. Now (and in the following articles), we will fully elaborate on another improved exception handling mechanism provided by the Windows operating system platform, that is the well-known structured exception handling (seh) programming method.

Seh design motivation

The following is a reference from a short section in window core programming:

"The main motivation for Microsoft to introduce seh in Windows is to facilitate the development of the operating system itself. Operating system developers use seh to make the system stronger. We can also use seh to make our own programs stronger. The burden caused by the use of seh is mainly borne by the Compilation Program, rather than the operating system. When an exception block occurs, the compiler generates special code. The compile program must generate some tables (t a B L E) to support processing the seh data structure. The compiler must also provide callback (c a l B A C K) functions. The operating system can call these functions to ensure that the exception block is processed. The Compilation Program is also responsible for preparing the stack structure and other internal information for the operating system to use and reference. Adding seh support to the Compilation Program is not easy. It is not surprising that different compiler manufacturers implement seh in different ways. Fortunately, we don't have to consider the implementation details of the Compilation Program, but only use the seh function of the Compilation Program ."

Indeed, the main motivation of seh design is to facilitate the development of the operating system itself. Why? This is because the operating system is a very large system, and it is still in the entire computer system, very underlying system software, therefore, the "Operating System" critical system software must be very strong and reliable. Of course, there are many effective methods to improve the reliability of the operating system software, such as rigorous design and comprehensive and scientific testing. But as the saying goes, "The wise may have a loss ". Therefore, there is a very effective Exception Handling Mechanism in programming code, which will greatly improve the reliability of the software system. Speaking of this, many friends may say, "Ah Yu! Are there any mistakes in C ++ or C that do not have a good exception handling mechanism? Why do we need to design another one ?". Indeed, it is true, but note that, due to efficiency considerations, the operating system often does not consider using the C ++ language for writing. Most of its modules are encoded in C language, it also contains a small part of assembly code. Therefore, in this way, only goto statements, setjmp, longjmp, and other mechanisms can be used in the operating system. But the designers of window think this is far from what they need. In addition, especially in the operating system, the largest software module is the device driver, and in the driver module of the device, most of them are developed by 3rd vendors (hardware providers. These driver modules developed by the third party are closely related to the core of the operating system, which seriously affects the reliability and stability of the entire operating system. Therefore, how can we make the driver modules developed by the 3rd party more reliable? At least it will not affect the normal operation of the operating system kernel, or even cause the crash of the operating system kernel. Objectively speaking, this is really hard to do because your fate is in the hands of others. However, the window designer came up with a very good method, that is, to provide the seh Mechanism for the driver developers on the 3rd side, to maximize the reliability and stability of the driver developed by 3rd. I believe that my friends who have developed drivers on the Windows platform are deeply touched by this, and most books on driver development strongly recommend that you use the seh mechanism in programming.

Now let's talk about why the setjmp and longjmp mechanisms in C language do not have the seh mechanism? Haha! Of course, it may be difficult to discuss and explain this issue well before you have a deep understanding of the seh mechanism. However, the hero a Yu believes that it is most appropriate to discuss this issue here. This can be a good way to follow the footsteps of seh designers, to find out their design ideas. At least we can fully analyze the shortcomings of setjmp and longjmp mechanisms. For example, in actual programming (especially System Programming), do you often encounter this situation? The sample code is as follows:

# Include <stdio. h>
# Include <setjmp. h>
# Include <stdlib. h>

Jmp_buf mark;

Void test1 ()
{
Char * P1, * P2, * P3, * P4;
P1 = malloc (10 );
If (! P1) longjmp (mark, 1 );

P2 = malloc (10 );
If (! P2)
{
// Although resources can be released,
// But it is easy for programmers to forget and make mistakes.
Free (P1 );
Longjmp (mark, 1 );
}

P3 = malloc (10 );
If (! P3)
{
// Although resources can be released here
// But it is easy for programmers to forget and make mistakes.
Free (P1 );
Free (P2 );
Longjmp (mark, 1 );
}

P4 = malloc (10 );
If (! P4)
{
// Although resources can be released here
// But it is easy for programmers to forget and make mistakes.
Free (P1 );
Free (P2 );
Free (P3 );
Longjmp (mark, 1 );
}

// Do other job

Free (P1 );
Free (P2 );
Free (P3 );
Free (P4 );
}

Void test ()
{
Char * P;
P = malloc (10 );

// Do other job

Test1 ();

// Do other job

// The resources here may not be released
Free (P );
}

Void main (void)
{
Int jmpret;

Jmpret = setjmp (Mark );
If (jmpret = 0)
{
Char * P;
P = malloc (10 );

// Do other job

Test1 ();

// Do other job
// The resources here may not be released
Free (P );
}
Else
{
Printf ("caught an exception/N ");
}
}

The above program can easily cause memory resource leakage, and the program code looks awkward. When programmers write code, they always release related resources very carefully. A slight improper handling may lead to resource leaks, or even worse, it may lead to system deadlocks, because when writing a driver, after all, it is very different from general applications. Some synchronization and mutex control among processes are often used.

What should I do? If there is a mechanism that can give programmers an appropriate opportunity to release these system resources when exceptions occur in the program, how nice it is! In fact, this requirement is a bit equivalent to the destructor in C ++, because it will always be executed whenever the object leaves the scope of its survival. However, the setjmp and longjmp mechanisms in C Language obviously cannot do this. With this in mind, seh designers provide this feature to program developers in the seh mechanism. This is actually the most powerful and distinctive part of Seh. In addition, seh also provides a mechanism for the program to be effectively restored after an exception occurs and the program continues to be executed, which is not available in other exception handling models (although generally, there is no need to do this; but it is sometimes necessary for system modules ).

What is seh?

First, we will reference the related discussions about seh in msdn as follows:

Windows 95 and Windows NT support a robust approach to handling exceptions, called structured exception handling, which involves cooperation of the operating system but also has direct support in the programming language.
An "exception" is an event that is unexpected or disrupts the ability of the process to proceed normally. exceptions can be detected by both hardware and software. hardware exceptions include dividing by zero and overflow of a numeric type. software exceptions include those you detect and signal to the system by calling the raiseexception function, and special situations detected by Windows 95 and Windows NT.
You can write more reliable code with structured exception handling. you can ensure that resources, such as memory blocks and files, are properly closed in the event of unexpected termination. you can also handle specific problems, such as insufficient memory, with concise, Structured code that doesn't rely on goto statements or elaborate testing of return codes.
Note these articles describe structured exception handling for the C programming language. although structured exception handling can also be used with C ++, the new C ++ Exception Handling Method shoshould be used for C ++ programs. see using structured exception handling with C ++ for information on special considerations.

As we all know, seh is a perfect exception handling mechanism provided by the Windows operating system platform. After all, this is a bit too abstract. For programmers, it should have a complete Exception Handling model consisting of several keywords, such as try, catch, and throw in C ++. Yes, that's true. seh also has similar syntax, that is, it consists of the following keywords:
_ Try
_ Limit t
_ Finally
_ Leave

Haha! Is this used by many programmers who develop on Windows platforms. Very familiar! But there is actually a misunderstanding here. Most programmers, including many computer books, use the seh mechanism and _ Try ,__ when T ,__ finally, __leave exception model is equivalent. This is wrong, at least not accurate. In a narrow sense, she can only be regarded as an exception handling mechanism provided by the Windows operating system, regardless of the _ Try, __partition T, __finally, __leave exception model, or try, catch, and throw exception models, which are implemented by VC and provided to developers. Among them, the _ Try ,__ partition t ,__ finally ,__ leave exception model is mainly provided for C programmers; while the try, catch, throw exception model, it is provided to C ++ programmers and complies with the definition of the exception model in the C ++ standard. The relationship between these exception mechanisms is shown in:

Summary

To further understand the seh mechanism, we need to know the differences between seh and the _ Try ,__ break t ,__ finally ,__ leave exception model mechanism. In the next article, the hero, a Yu, came up with a specific example program to share with you. Note that in this example, it neither uses the _ Try, ____t, __finally, __leave exception model, nor uses the try, catch, and throw exception models. It directly uses the seh mechanism provided by the Windows operating system and completes a simple encapsulation. Continue! Go!

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.