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: GIf 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: Fend 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:
| |
Jagger Software Ltd | |
Company # 4070126 | |
VAT # 762 5213 42 |