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 thenext 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 calling
ExitProcess,
ExitThread,
TerminateProcess,
or
TerminateThread). 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)
分享到:
相关推荐
Chapter 23: Timers and Sleeping Chapter 24: Process Creation Chapter 25: Process Termination Chapter 26: Monitoring Child Processes Chapter 27: Program Execution Chapter 28: Process Creation and ...
MOLAND: Termination of the consent solicitation.pdf
Termination of Coverage.pdf
Chapter 1: The Microsoft Developer Studio The Microsoft Developer Studio The Project Workspace Project Workspace Window Project Configurations Managing Complex Projects Project Settings ...
Chapter 1. Introduction Section 1.1. Introduction Section 1.2. A Simple Daytime Client Section 1.3. Protocol Independence Section 1.4. Error Handling: Wrapper Functions Section 1.5. A ...
Advanced Process Termination 2.1 用多种方法结束进程 一般用于测试安全工具的自我保护的测试
category: 1234, operation: scls_process_join, loc: childcrash, OS error: 0, other: Abnormal termination of the child 2015-09-12 06:51:03.602: [ CRSEVT][11304]32CAAMonitorHandler :: 0:Action Script /...
一份描述网口Bob Smith的电路,对于了解此电路的功能有很大的帮助
Atom-Termination.zip,Integrated terminal for Atom. Looks like terminal-plus, acts like your native terminal (except every other Friday). Looking for collaborators! :-)终止,atom是一个用web技术构建的...
In the past there have been two main termination specifications for UTP data cable infrastructure and yet another for phone cable infrastructure. In today’s world of structured cable systems where ...
一篇关于x264快速自适应模式选择的论文:Fast Adaptive Early Termination for Mode Selection in H.264AVC Standard Based on x264 Implementation
ecs-spot-termination配置集 此配置集安装了一个现场终止脚本,该脚本将连续运行并监视现场两分钟警告。 看到两分钟的警告后,它将从ECS群集中耗尽实例。什么是lono配置集? Lono配置集允许通常嵌入在模板中的 可...
Chapter 13 Duration and Termination Chapter 14 Insurance Chapter 15 Supplementary Provisions Chapter 1 General Provisions Article 1 In accordance with Law of the People's Republic of China on Foreign-...
使用这个程序可以轻易关闭著名杀毒软件、系统关键进程,但使用前请关闭杀毒软件&防火墙,以免误杀。
Transmission lines equations for coupled lines symmetrical lines and symmetrically driven homogeneous medium symmetrical lines in homogeneous medium symmetrical lines and asymmetrical driven ...
目的 应用程序是一种确保可抢占节点在将要被抢占以便Kubernetes更新其管理时接收信号的方法。 没有节点就迷路了,Kubernetes需要一段时间才能意识到它已经消失并更新了管理。 同时,请求可以继续发送到不再存在的Pod...
Bob Smith Termination patent
1 Introduction 2 Representing processes in Linux 3 Organising the task structures 4 Wait queues 5 Mutual exclusion with locks 6 Mutual exclusion with waiting 7 Scheduling 8 Process creation 9 Process ...