Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 15 Statementsprevious at this levelnext at this level 15.10 The try statement Paragraph 11 The try statement provides a mechanism for catching exceptions that occur during execution of a block. Paragraph 21 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 Paragraph 31 There are three possible forms of try statements: Paragraph 41 When a catch clause specifies a class-type, the type must be System.Exception or a type that derives from System.Exception. Paragraph 51 When a catch clause specifies both a class-type and an identifier, an exception variable of the given name and type is declared. 2 The exception variable corresponds to a local variable with a scope that extends over the catch block. 3 During execution of the catch block, the exception variable represents the exception currently being handled. 4 For purposes of definite assignment checking, the exception variable is considered definitely assigned in its entire scope. Paragraph 61 Unless a catch clause includes an exception variable name, it is impossible to access the exception object in the catch block. Paragraph 71 A catch clause that specifies neither an exception type nor an exception variable name is called a general catch clause. 2 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] Paragraph 81 In order to locate a handler for an exception, catch clauses are examined in lexical order. 2 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] Paragraph 91 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. 2 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]
Paragraph 101 It is a compile-time error for a break, continue, or goto statement to transfer control out of a finally block. 2 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. Paragraph 111 It is a compile-time error for a return statement to occur in a finally block. Paragraph 121 A try statement is executed as follows: Paragraph 131 The statements of a finally block are always executed when control leaves a try statement. 2 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. Paragraph 141 If an exception is thrown during execution of a finally block, the exception is propagated to the next enclosing try statement. 2 If another exception was in the process of being propagated, that exception is lost. 3 The process of propagating an exception is discussed further in the description of the throw statement (§15.9.5). Paragraph 151 The try block of a try statement is reachable if the try statement is reachable. Paragraph 161 A catch block of a try statement is reachable if the try statement is reachable. Paragraph 171 The finally block of a try statement is reachable if the try statement is reachable. Paragraph 181 The end point of a try statement is reachable if both of the following are true:
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS