Throw statement
Before we learn how to handle exceptions, let's introduce the throw statement.
The throw statement throws an exception:
Throw expression
The exception that is thrown by the throw statement with an expression is generated when the expression is evaluated. This expression must represent a System.Exception type or a value of its derived type. If the evaluation of an expression produces a null result, a NullReferenceException exception is thrown.
Throw statement without an expression we'll introduce you later.
Exception Handling Statements
The exception is handled by a try statement.
The Try statement provides a mechanism to catch exceptions that occur during block execution. Here are three possible forms of it:
Try-catch (s)
Try-finally
Try-catch (s)-finally
Before introducing the try statement, let's first introduce an important concept: abnormal propagation. When an exception is thrown, the program transfers control to the first catch clause in the TRY statement that can handle the exception. The process of throwing from an exception to a control transfer to an appropriate exception-handling statement is called abnormal propagation. This article is published in www.bianceng.cn (programming entry Network)
Exception propagation involves performing the following steps repeatedly until a catch clause matching the exception is found.
(1) A try statement that executes each enclosing throw point (the initial position of the exception being thrown) from the layer to the outer.
If the current try block contains one or more catch clauses, they are checked individually to locate the processing that is appropriate for the exception, in the order in which they appear. The handling of the so-called appropriate exception is the first catch statement that defines the exception type or its base type, and the exception propagation ends, and the program control transfers to the Catch statement execution.
Otherwise, if the current try block contains a finally block, the finally block is executed. If the block throws an exception again, the currently processed exception is terminated. If not, then continue with the exception processing when the finally block execution completes.
(2). The call terminates if there is no positional exception handling in the current member function call. and throws the exception to the caller at the member function call, repeating the previous step.
(3). If the exception terminates all the member function calls of the current thread or process, the exception is not handled in the thread or process, and it terminates itself.
Now let's go back to the try statement
If a class type is specified in a catch statement, it must be a System.Exception type or its derived type. If both the class type and the identifier are specified, an exception variable is declared. An exception variable is equivalent to a local variable scoped to the entire catch block. During the execution of a catch block, the exception variable describes the exception that is currently being handled. If you want to reference an exception object, which includes many important error messages, you must define the exception variable.
In a catch block, an exception that is caught by the catch block can be thrown again with an throw statement that does not contain an expression, and the allocation of an exception variable does not change the exception thrown two times.
A catch clause that neither defines an exception type nor defines an exception variable is called a general catch clause. The catch clause is generally used in cases where the exception cannot be determined beforehand. There can be only one general catch clause in a try statement, and if so, the catch clause must be followed by other catch clauses.
Although a throw statement (with an expression) can only throw an exception of a System.Exception type or its derived type, other statements are not restricted by the rule, and other types of exceptions can be thrown. For example, you can catch this type of exception with a general catch statement, and then throw it back with a throw statement that does not contain an expression.
Because the process of seeking exceptions is to check the catch clause in the order in which the catch clause appears, an error occurs if a catch clause defines an exception type that is the same type as or a derived type that is defined by a catch clause that appears first. In the following example we demonstrate the use of the Try-catch statement and throw the exception again.
Program Listing 8-9:
Using System;
Class Test
{
static void F () {
try{
G ();
}
catch (Exception e) {
Console.WriteLine ("Exception in F:" +e.message);
E=new Exception ("F");
Throw
}
}
static void G () {
throw new Exception ("G");
}
static void Main () {
try{
F ();
}
catch (Exception) {
Console.WriteLine ("Exception in Main:" +e.message);}}
The F method captures an exception, writes some diagnostic information to the console, alters the contents of the exception variable, and then throws the exception again. The exception that was thrown again is the same as the exception that was initially caught. Therefore, the output of the program is as follows:
Exception in F:g
Exception in Main:g