Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

CATEGORII DOCUMENTE





AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml


The try statement

C sharp

+ Font mai mare | - Font mai mic







DOCUMENTE SIMILARE

Trimite pe Messenger
Operators - Operator precedence and associativity
Shift operators
Classes
Blocks
Automatic memory management
Variable categories
Exceptions - Causes of exceptions
The empty statement
Jump statements
Attributes - Attribute classes

The try statement

The try statement provides a mechanism for catching exceptions that occur during execution of a block. The try statement furthermore 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 the purpose 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.

Though the throw statement is restricted to throwing exceptions of type System.Exception or a type that derives from System.Exception, other languages are not bound by this rule, and so may throw exceptions of other types. A general catch clause can be used to catch such exceptions, and a throw statement with no expression can be used to re-throw them.

In order to locate a handler for an exception, catch clauses are examined in lexical order. An error occurs if a catch clause specifies a type that is the same as or derived from a type that was specified in an earlier catch clause for the same try. Without this restriction it would be possible to write unreachable catch clauses.

Within a catch block, a throw statement (8.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.

In the example

class Test

catch (Exception e)
}

static void G()

static void Main()
catch (Exception e)
}
}

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 of the program 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 of the program would be as follows:

Exception in F: G
Exception in Main: F

It is an 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 an 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 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:

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 catch block:

If the try statement has a finally block, the finally block is executed.

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 (8.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.

The checked and unchecked statements

The checked and unchecked statements are used to control the overflow checking context for integral-type arithmetic operations and conversions.

checked-statement:
checked block

unchecked-statement:
unchecked block

The checked statement causes all expressions in the block to be evaluated in a checked context, and the unchecked statement causes all expressions in the block to be evaluated in an unchecked context.

The checked and unchecked statements are precisely equivalent to the checked and unchecked operators (7.5.12), except that they operate on blocks instead of expressions.








Politica de confidentialitate

DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 290
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2019 . All rights reserved

Distribuie URL

Adauga cod HTML in site