分享
 
 
 

15.10 The try statement

王朝other·作者佚名  2006-05-06
窄屏简体版  字體: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有