try-except 语句 (C)try-except Statement (C)

Microsoft 专用Microsoft Specific

try-except 语句是一项 Microsoft C++ 语言扩展,它使应用程序能够在正常终止执行的事件发生时获取对程序的控制权。The try-except statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. 此类事件称为异常,处理异常的机制称为结构化异常处理。Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.

异常可能基于硬件或软件。Exceptions can be either hardware- or software-based. 即使应用程序无法从硬件或软件异常中完全恢复,结构化异常处理也可以显示错误信息并捕获应用程序的内部状态,从而帮助诊断问题。Even when applications cannot completely recover from hardware or software exceptions, structured exception handling makes it possible to display error information and trap the internal state of the application to help diagnose the problem. 这对于无法轻松重现的间歇性问题特别有用。This is especially useful for intermittent problems that cannot be reproduced easily.

语法Syntax

try-except-statement:try-except-statement:
__try compound-statement__try compound-statement

__except ( expression ) compound-statement__except ( expression ) compound-statement

__try 子句后的复合语句是受保护节。The compound statement after the __try clause is the guarded section. __except 子句后的复合语句是异常处理程序。The compound statement after the __except clause is the exception handler. 如果在控制节执行过程中引发了异常,处理程序将指定要采取的一系列措施。The handler specifies a set of actions to be taken if an exception is raised during execution of the guarded section. 执行过程如下所示:Execution proceeds as follows:

  1. 执行受保护节。The guarded section is executed.

  2. 如果在受保护节执行过程中未发生异常,则继续执行 __except 子句之后的语句。If no exception occurs during execution of the guarded section, execution continues at the statement after the __except clause.

  3. 如果在受保护节的执行过程中或受保护节调用的任何例程中发生异常,则会计算 __except 表达式,返回的值将确定该异常的处理方式。If an exception occurs during execution of the guarded section or in any routine the guarded section calls, the __except expression is evaluated and the value returned determines how the exception is handled. 有三个值:There are three values:

    EXCEPTION_CONTINUE_SEARCH 异常无法识别。EXCEPTION_CONTINUE_SEARCH Exception is not recognized. 继续向上搜索堆栈查找处理程序,首先是所在的 try-except 语句,然后是具有下一个最高优先级的处理程序。Continue to search up the stack for a handler, first for containing try-except statements, then for handlers with the next highest precedence.

    EXCEPTION_CONTINUE_EXECUTION 异常可识别,但被关闭。EXCEPTION_CONTINUE_EXECUTION Exception is recognized but dismissed. 从出现异常的点继续执行。Continue execution at the point where the exception occurred.

    EXCEPTION_EXECUTE_HANDLER 异常可识别。EXCEPTION_EXECUTE_HANDLER Exception is recognized. 通过执行 __except 复合语句来转移对异常处理程序的控制,然后在异常发生处继续执行。Transfer control to the exception handler by executing the __except compound statement, then continue execution at the point the exception occurred.

    由于 __except 表达式将作为 C 表达式来计算,因此它被限制为单个值、条件表达式运算符或逗号运算符。Because the __except expression is evaluated as a C expression, it is limited to a single value, the conditional-expression operator, or the comma operator. 如果需要更大量的处理,表达式可调用返回上面列出的三个值之一的例程。If more extensive processing is required, the expression can call a routine that returns one of the three values listed above.

备注

结构化异常处理适用于 C 和 C++ 源文件。Structured exception handling works with C and C++ source files. 但是,这不是专门为 C++ 设计的。However, it is not specifically designed for C++. 您可通过使用 C++ 异常处理来确保提高代码的可移植性。You can ensure that your code is more portable by using C++ exception handling. 此外,C++ 异常处理机制灵活得多,因为它可以处理任何类型的异常。Also, the C++ exception handling mechanism is much more flexible, in that it can handle exceptions of any type.

备注

对于 C++ 程序,应使用 C++ 异常处理,而不是结构化异常处理。For C++ programs, C++ exception handling should be used instead of structured exception handling. 有关详细信息,请参阅《C++ 语言参考》中的异常处理For more information, see Exception Handling in the C++ Language Reference.

应用程序中的每个例程可以有自己的异常处理程序。Each routine in an application can have its own exception handler. __except 表达式在 __try 体的范围内执行。The __except expression executes in the scope of the __try body. 这意味着它可以访问在该处声明的任何局部变量。This means it has access to any local variables declared there.

__leave 关键字在 try-except 语句块中有效。The __leave keyword is valid within a try-except statement block. __leave 的效果是跳转到 try-except 块的末尾。The effect of __leave is to jump to the end of the try-except block. 执行将在异常处理程序结束后恢复。Execution resumes after the end of the exception handler. 尽管可使用 goto 语句来达到相同的结果,但 goto 语句会导致堆栈展开。Although a goto statement can be used to accomplish the same result, a goto statement causes stack unwinding. 由于 __leave 语句不涉及堆栈展开,因此更有效。The __leave statement is more efficient because it does not involve stack unwinding.

使用 longjmp 运行时函数退出 try-except 语句被视为异常终止。Exiting a try-except statement using the longjmp run-time function is considered abnormal termination. 跳转到 __try 语句是非法的,但跳出该语句是合法的。It is illegal to jump into a __try statement, but legal to jump out of one. 如果有进程在执行 try-except 语句的过程中终止,则不会调用异常处理程序。The exception handler is not called if a process is killed in the middle of executing a try-except statement.

示例Example

下面是异常处理程序和终止处理程序的示例。Following is an example of an exception handler and a termination handler. 有关终止处理程序的详细信息,请参阅 try-finally 语句See The try-finally Statement for more information about termination handlers.

.  
.  
.  
puts("hello");  
__try{  
   puts("in try");  
   __try{  
      puts("in try");  
      RAISE_AN_EXCEPTION();  
   }__finally{  
      puts("in finally");  
   }  
}__except( puts("in filter"), EXCEPTION_EXECUTE_HANDLER ){  
   puts("in except");  
}  
puts("world");  

这是上面的示例的输出,右侧还添加了注释:This is the output from the example, with commentary added on the right:

hello  
in try              /* fall into try                     */  
in try              /* fall into nested try                */  
in filter           /* execute filter; returns 1 so accept  */  
in finally          /* unwind nested finally                */  
in except           /* transfer control to selected handler */  
world               /* flow out of handler                  */  

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

try-except 语句try-except Statement