X hits on this document

1031 views

0 shares

0 downloads

0 comments

196 / 396

Chapter ‎18   Unsafe code

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

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 is would be as follows:

Exception in F: G Exception in Main: F

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.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.183

Document info
Document views1031
Page views1031
Page last viewedSat Dec 10 03:46:02 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments