The try statement provides a mechanism for catching exceptions that occur
during execution of a block.
Furthermore, the try statement provides the ability to specify a block of
code that is always executed when
control leaves the try statement.
try-statement:
try block catch-clauses
try block finally-clause
try block catch-clauses finally-clause
catch-clauses:
specific-catch-clauses general-catch-clauseopt
specific-catch-clausesopt general-catch-clause
specific-catch-clauses:
specific-catch-clause
specific-catch-clauses specific-catch-clause
specific-catch-clause:
catch ( class-type identifieropt ) block
general-catch-clause:
catch block
finally-clause:
finally block
There are three possible forms of try statements:
? A try block followed by one or more catch blocks.
? A try block followed by a finally block.
? A try block followed by one or more catch blocks followed by a finally
block.
When a catch clause specifies a class-type, the type must be
System.Exception or a type that derives
from System.Exception.
When a catch clause specifies both a class-type and an identifier, an
exception variable of the given name
and type is declared. The exception variable corresponds to a local
variable with a scope that extends over
the catch block. During execution of the catch block, the exception
variable represents the exception
currently being handled. For purposes of definite assignment checking, the
exception variable is considered
definitely assigned in its entire scope.
Unless a catch clause includes an exception variable name, it is impossible
to access the exception object
in the catch block.
A catch clause that specifies neither an exception type nor an exception
variable name is called a general
catch clause. A try statement can only have one general catch clause, and
if one is present it must be the
last catch clause.
[Note: Some environments, especially those supporting multiple languages,
may support exceptions that are
not representable as an object derived from System.Exception, although such
an exception could never
be generated by C# code. In such an environment, a general catch clause
might be used to catch such an
exception. Thus, a general catch clause is semantically different from one
that specifies the type
System.Exception, in that the former may also catch exceptions from other
languages. end note]
In order to locate a handler for an exception, catch clauses are examined
in lexical order. A compile-time
error occurs if a catch clause specifies a type that is the same as, or is
derived from, a type that was
specified in an earlier catch clause for the same try. [Note: Without this
restriction, it would be possible to
write unreachable catch clauses. end note]
C# LANGUAGE SPECIFICATION
196
Within a catch block, a throw statement (§15.9.5) with no expression can
be used to re-throw the
exception that was caught by the catch block. Assignments to an exception
variable do not alter the
exception that is re-thrown.
[Example: In the example
using System;
class Test
{
static void F() {
try {
G();
}
catch (Exception e) {
Console.WriteLine("Exception in F: " + e.Message);
e = new Exception("F");
throw; // re-throw
}
}
static void G() {
throw new Exception("G");
}
static void Main() {
try {
F();
}
catch (Exception e) {
Console.WriteLine("Exception in Main: " + e.Message);
}
}
}
the method F catches an exception, writes some diagnostic information to
the console, alters the exception
variable, and re-throws the exception. The exception that is re-thrown is
the original exception, so the output
produced is:
Exception in F: G
Exception in Main: G
If the first catch block had thrown e instead of rethrowing the current
exception, the output produced would
be as follows:
Exception in F: G
Exception in Main: F
end example]
It is a compile-time error for a break, continue, or goto statement to
transfer control out of a finally
block. When a break, continue, or goto statement occurs in a finally block,
the target of the statement
must be within the same finally block, or otherwise a compile-time error
occurs.
It is a compile-time error for a return statement to occur in a finally
block.
A try statement is executed as follows:
? Control is transferred to the try block.
? When and if control reaches the end point of the try block:
If the try statement has a finally block, the finally block is executed.
Control is transferred to the end point of the try statement.
? If an exception is propagated to the try statement during execution of
the try block:
The catch clauses, if any, are examined in order of appearance to locate a
suitable handler for the
exception. The first catch clause that specifies the exception type or a
base type of the exception type is
Chapter 15 Statements
197
considered a match. A general catch clause is considered a match for any
exception type. If a matching
catch clause is located:
? If the matching catch clause declares an exception variable, the
exception object is assigned to
the exception variable.
? Control is transferred to the matching catch block.
? When and if control reaches the end point of the catch block:
o If the try statement has a finally block, the finally block is executed.
o Control is transferred to the end point of the try statement.
? If an exception is propagated to the try statement during execution of
the catch block:
o If the try statement has a finally block, the finally block is executed.
o The exception is propagated to the next enclosing try statement.
If the try statement has no catch clauses or if no catch clause matches the
exception:
? If the try statement has a finally block, the finally block is executed.
? The exception is propagated to the next enclosing try statement.
The statements of a finally block are always executed when control leaves a
try statement. This is true
whether the control transfer occurs as a result of normal execution, as a
result of executing a break,
continue, goto, or return statement, or as a result of propagating an
exception out of the try statement.
If an exception is thrown during execution of a finally block, the
exception is propagated to the next
enclosing try statement. If another exception was in the process of being
propagated, that exception is lost.
The process of propagating an exception is discussed further in the
description of the throw statement
(§15.9.5).
The try block of a try statement is reachable if the try statement is
reachable.
A catch block of a try statement is reachable if the try statement is
reachable.
The finally block of a try statement is reachable if the try statement is
reachable.
The end point of a try statement is reachable if both of the following are
true:
? The end point of the try block is reachable or the end point of at least
one catch block is reachable.
? If a finally block is present, the end point of the finally block is
reachable.
分享到:
相关推荐
ue15.10注册码ue15.10注册码ue15.10注册码ue15.10注册码ue15.10注册码ue15.10注册码ue15.10注册码ue15.10注册码ue15.10注册码
【UE15.10编辑器详解】 UE15.10,全称为UEditor 15.10,是一款备受赞誉的文本编辑器,以其高效、强大的功能和对系统资源的低占用而受到广大用户的喜爱。这款编辑器不仅适用于程序员进行代码编写,也适合普通用户...
《BCGControlBar Professional v15.10 完整源码详解》 BCGControlBar Professional是一款由BCGSoft公司开发的专业级MFC扩展库,版本号为v15.10。这个库提供了丰富的用户界面元素和控件,极大地增强了Microsoft ...
phpMyAdmin-4.4.15.10-all-languages.zip
ubuntu15.10中火狐可用最新的flash
uedit15.10+注册机
Ultra Edit 15.10.0.1031 注册码, 经过自己的适用.可以使用.
【船级社】 RINA GUI40 - 15.10.2021 Guide for the Evaluation of Energy Efficiency Existing Ship Index (EEXI).pdf
为了解决这一问题,我们引入了“Frontline15.10-18.11.17745.19732_signed.exe.7z”——一款名为FTS4BT(Frontline Protocol Analysis System)的蓝牙抓包与分析软件。 FTS4BT是一款专业级的蓝牙协议分析工具,它...
《gcompris-15.10.zip:儿童教育软件GCompris的Windows版本详解》 GCompris是一款专为儿童设计的开源教育软件,它包含了丰富的学习活动,旨在激发孩子们的兴趣,帮助他们在玩耍中学习。"gcompris-15.10.zip" 文件是...
V15.10.05.21 O1012是该主控对应的一款固件更新和量产工具,用于对U盘进行各种维护和优化操作。这个工具是中文版,方便中国用户更加便捷地理解和使用。 "U盘量产工具"是专门用来对U盘进行批量生产设置的软件,它...
15.10.xps
Base_EXT15.10.000_lnx86_1of2.tar
安装步骤:1.导入bcgcontrolbarpro.v15.10.reg 2.运行BCGCBProIntegrationWizard.exe,编译各动态库 3.查看[使用说明]
Ultra edit 15.10.1026 中文注册码 Ultra edit 15.10.1026 中文注册码
在探讨“UlteraEdit 15.10 注册码”的话题时,我们不仅局限于注册码本身,更重要的是理解UlteraEdit这款软件的功能、特点以及为何用户可能需要一个注册码来激活其完整功能。 ### UlteraEdit:一款多功能文本编辑器 ...
ubuntu15.10安装samba的配置文件
15.10、kubernetes -- 04.pdf
在本文中,我们将详细探讨如何在Ubuntu 15.10操作系统上安装并配置Swift开发环境。Swift是由苹果公司开发的一种编程语言,旨在替代Objective-C成为构建Mac OS X和iOS应用的主要工具。自Swift开源以来,它已经逐渐...