<?xml version="1.0"?>
<clause number="15.10" title="The try statement">
  <paragraph>The try statement provides a mechanism for catching exceptions that occur during execution of a block. </paragraph>
  <paragraph>Furthermore, the try statement provides the ability to specify a block of code that is always executed when control leaves the try statement. <grammar_production><name><non_terminal where="15.10">try-statement</non_terminal></name> : <rhs><keyword>try</keyword><non_terminal where="15.2">block</non_terminal><non_terminal where="15.10">catch-clauses</non_terminal></rhs><rhs><keyword>try</keyword><non_terminal where="15.2">block</non_terminal><non_terminal where="15.10">finally-clause</non_terminal></rhs><rhs><keyword>try</keyword><non_terminal where="15.2">block</non_terminal><non_terminal where="15.10">catch-clauses</non_terminal><non_terminal where="15.10">finally-clause</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="15.10">catch-clauses</non_terminal></name> : <rhs><non_terminal where="15.10">specific-catch-clauses</non_terminal><non_terminal where="15.10">general-catch-clause</non_terminal><opt/></rhs><rhs><non_terminal where="15.10">specific-catch-clauses</non_terminal><opt/><non_terminal where="15.10">general-catch-clause</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="15.10">specific-catch-clause</non_terminal>s</name> : <rhs><non_terminal where="15.10">specific-catch-clause</non_terminal></rhs><rhs><non_terminal where="15.10">specific-catch-clauses</non_terminal><non_terminal where="15.10">specific-catch-clause</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="15.10">specific-catch-clause</non_terminal></name> : <rhs><keyword>catch</keyword><terminal>(</terminal><non_terminal where="11.2">class-type</non_terminal><non_terminal where="9.4.2">identifier</non_terminal><opt/><terminal>)</terminal><non_terminal where="15.2">block</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="15.10">general-catch-clause</non_terminal></name> : <rhs><keyword>catch</keyword><non_terminal where="15.2">block</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="15.10">finally-clause</non_terminal></name> : <rhs><keyword>finally</keyword><non_terminal where="15.2">block</non_terminal></rhs></grammar_production></paragraph>
  <paragraph>There are three possible forms of try statements: <list><list_item> A try block followed by one or more catch blocks. </list_item><list_item> A try block followed by a finally block. </list_item><list_item> A try block followed by one or more catch blocks followed by a finally block. </list_item></list></paragraph>
  <paragraph>When a catch clause specifies a <non_terminal where="11.2">class-type</non_terminal>, the type must be System.Exception or a type that derives from System.Exception. </paragraph>
  <paragraph>When a catch clause specifies both a <non_terminal where="11.2">class-type</non_terminal> 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. </paragraph>
  <paragraph>Unless a catch clause includes an exception variable name, it is impossible to access the exception object in the catch block. </paragraph>
  <paragraph>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. </paragraph>
  <paragraph>
    <note>[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]</note>
  </paragraph>
  <paragraph>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>[Note: Without this restriction, it would be possible to write unreachable catch clauses. end note]</note> </paragraph>
  <paragraph>Within a catch block, a throw statement (<hyperlink>15.9.5</hyperlink>) 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. </paragraph>
  <paragraph>
    <example>[Example: In the example <code_example><![CDATA[
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);  
      }  
   }  
}  
]]></code_example>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: <code_example><![CDATA[
Exception in F: G  
Exception in Main: G  
]]></code_example></example>
  </paragraph>
  <paragraph>
    <example>If the first catch block had thrown e instead of rethrowing the current exception, the output produced would be as follows: <code_example><![CDATA[
Exception in F: G  
Exception in Main: F  
]]></code_example>end example]</example>
  </paragraph>
  <paragraph>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. </paragraph>
  <paragraph>It is a compile-time error for a return statement to occur in a finally block. </paragraph>
  <paragraph>A try statement is executed as follows: <list><list_item> Control is transferred to the try block. </list_item><list_item> When and if control reaches the end point of the try block: </list_item><list><list_item> If the try statement has a finally block, the finally block is executed. </list_item><list_item> Control is transferred to the end point of the try statement. </list_item></list><list_item> If an exception is propagated to the try statement during execution of the try block: </list_item><list><list_item> 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: </list_item><list><list_item> If the matching catch clause declares an exception variable, the exception object is assigned to the exception variable. </list_item><list_item> Control is transferred to the matching catch block. </list_item><list_item> When and if control reaches the end point of the catch block: </list_item><list><list_item> If the try statement has a finally block, the finally block is executed. </list_item><list_item> Control is transferred to the end point of the try statement. </list_item></list><list_item> If an exception is propagated to the try statement during execution of the catch block: </list_item><list><list_item> If the try statement has a finally block, the finally block is executed. </list_item><list_item> The exception is propagated to the next enclosing try statement. </list_item></list></list><list_item> If the try statement has no catch clauses or if no catch clause matches the exception: </list_item><list><list_item> If the try statement has a finally block, the finally block is executed. </list_item><list_item> The exception is propagated to the next enclosing try statement. </list_item></list></list></list></paragraph>
  <paragraph>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. </paragraph>
  <paragraph>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 (<hyperlink>15.9.5</hyperlink>). </paragraph>
  <paragraph>The try block of a try statement is reachable if the try statement is reachable. </paragraph>
  <paragraph>A catch block of a try statement is reachable if the try statement is reachable. </paragraph>
  <paragraph>The finally block of a try statement is reachable if the try statement is reachable. </paragraph>
  <paragraph>The end point of a try statement is reachable if both of the following are true: <list><list_item> The end point of the try block is reachable or the end point of at least one catch block is reachable. </list_item><list_item> If a finally block is present, the end point of the finally block is reachable. </list_item></list></paragraph>
</clause>
