现在的位置: 首页 > 综合 > 正文

Chapter 23: Termination Handlers(1)Overview

2018年04月10日 ⁄ 综合 ⁄ 共 4084字 ⁄ 字号 评论关闭

Close your eyes for a moment and imagine writing your application as though your code could never fail. That's right—there's always enough memory, no one ever passes you an invalid pointer, and the files you count on always exist. Wouldn't
it be a pleasure to write your code if you could make these assumptions? Your code would be so much easier to write, to read, and to understand. No more fussing withif statements
here andgotos there—in each function, you'd just write your code top to bottom.

If this kind of straightforward programming environment seems like a dream to you, you'll love structured exception handling (SEH). The virtue of SEH is that as you write your code, you can focus on getting your task done. If something
goes wrong at run time, the system catches it and notifies you of the problem.

SEH的长处在于你可以专注在你的业务上,如果在运行时有什么错误发生,系统会捕获它并通知你。

With SEH, you can't totally ignore the possibility of an error in your code, but you can separate the main job from the error-handling chores. This division makes it easy to concentrate on the problem at hand and focus on possible errors later.

借助SEH,你不能完全忽视发生错误的可能性,但是你可以把主要业务流程和错误处理分开。

One of Microsoft's main motivations for adding SEH to Windows was to ease the development of the operating system itself. The developers of the operating system use SEH to make the system more robust. We can use SEH to make our own applications
more robust.

微软在Windows中添加SEH的动机之一是简化操作系统开发。操作系统的开发者通过使用SEH使得系统更加健壮,你可以用SEH使自己的程序更加健壮。

The burden of getting SEH to work falls more on the compiler than on the operating system. Your compiler must generate special code when exception blocks are entered into and exited from. The compiler must produce tables of support data structures
to handle SEH. The compiler also must supply callback functions that the operating system can call so that exception blocks can be traversed. And the compiler is responsible for preparing stack frames and other internal information that is used and referenced
by the operating system. Adding SEH support to a compiler is not an easy task. It shouldn't surprise you that different compiler vendors implement SEH in different ways. Fortunately, we can ignore compiler implementation details and just use the compiler's
SEH capabilities.

获得SEH特性主要给编译器带来了负担。你的编译器必须为异常产生特别的代码。编译器必须为支持SEH构造一些表。编译器还必须提供回调函数,操作系统调用它们以便异常块可以被traversed。

Differences among compiler implementations could make it difficult to discuss the advantages of SEH in specific ways with specific code examples. However, most compiler vendors follow Microsoft's suggested syntax. The syntax and keywords I use
in my examples might differ from those of another company's compiler, but the main SEH concepts are the same. I'll use the Microsoft Visual C++ compiler's syntax throughout this chapter.

本章中使用VC++编译器。

  Note 

Don't confuse structured exception handling with C++ exception handling. C++ exception handling is a different form of exception handling, a form that makes use of the C++ keywordscatch
andthrow. Microsoft's Visual C++ also supports C++ exception handling and is implemented internally by taking advantage of the structured exception handling capabilities already
present in the compiler and in Windows operating systems.

不要搞混结构化异常处理和C++异常处理。C++异常处理是另一种异常处理,使用C++关键字catch和throw。VC也支持C++异常处理,其实现是借助编译器和操作系统提供的结构化异常处理。

SEH really consists of two main capabilities: termination handling and exception handling. We'll discuss termination handlers in this chapter and exception handling in the next chapter.

SEH实际有两方面的能力:处理结束和处理异常。本章将讨论处理结束。

A termination handler guarantees that a block of code (the termination handler) will be called and executed regardless of how another section of code (the guarded body) is exited. The syntax (using the Microsoft Visual C++ compiler) for a termination
handler is as follows:

结束处理器可以确保无论发生了什么,Termination handler中的部分都会被执行。VC++下,语法如下

__try {
   // Guarded body 监视部分
   ...
}
__finally {
   // Termination handler
   ...
}

The __try and__finally keywords delineate the two sections of the termination handler. In the preceding
code fragment, the operating system and the compiler work together to guarantee that the__finally block code in the termination handler will be executed no matter how the guarded
body is exited (except if the process or the thread are terminated by callingExitProcess,ExitThread,TerminateProcess,
orTerminateThread). Regardless of whether you put a return, a goto,
or even a call to longjump in the guarded body, the termination handler will be called. I'll show you several examples demonstrating this.

关键字__try和__finally勾画出了termination handler的两个部分。在前面的代码片段里,OS和编译器一起保证无论guarded body中的代码怎么退出,__finally中的代码都会被执行。(除了进程或者线程被以下函数结束:ExitProcess,ExitThread,TerminateProcess,
orTerminateThread

抱歉!评论已关闭.