2.1. 介绍 Added into Delphi back at the very beginning with Delphi 1, exception handling was a fundamental change to the way we thought about and wrote our code. 自 delphi1 开始加入的异常处理,从根本上改变了我们的思考和代码编写的方式。 Unfortunately, despite almost twenty years of use, there are still many misconceptions and misunderstandings about how exceptions work and especially about how they should be used. 不幸的是 尽管使用了近 20 年,对异常的原理特别怎么样使用方面还存在着很多的误解。 In this chapter, I’ll look at how exception handling should be done. 在这一章,我会着眼于应该怎样进行异常处理。 I’ll start out with examples of the wrong way to use and handle exceptions. 我会用错误的方式使用和处理异常的例子开始。 Using that as a base, I’ll then discuss proper ways for using exception handling. 依次为基础,我会讨论正确使用异常处理的方式。 Used improperly, exception handling can actually cause more problems and errors than it prevents. Used correctly, they can aid you in writing clean, well-designed code that is easy to maintain. I’ll assume that you are familiar with exception handling syntax, and the basics of how exceptions work. 使用不当,异常处理实际上会导致更多的问题和错误比预防。使用得当,他可以辅助你编写 干净的、设计巧妙且易于维护的代码。本章假定你已经熟悉的异常处理的语法和了解异常的 基本工作原理。
An exception is a language feature that allows a programmer to stop execution of a process or thread immediately, but intercept that “stop” at any point in the call stack when necessary. Structured exception handling is a combination of language features and good design that makes use of exceptions in order to allow programmers to make useful assumptions when writing code and, most critically, respond correctly when those assumptions turn out not to be true. 异常是一种语言特性(它允许程序员在一个进程或者线程中立即停止执行), 但是当有需 要的时候拦截那个"停止"在调用堆栈的任何一个位置。 结构化异常处理是语言特征和良好设计的组合,利用异常是为了让程序员在编写代码的时候 做有用的假设,最重要的是当这些假设不成立时做出正确的回应。