The Raise statement raises an exception.
raise-statement:
Raise expressionopt
The Raise statement with the specified expression raises an exception with the value obtained as a result of calculating the expression. This value must represent an instance of the object type, which matches or derives from System.Exception. If the result of calculating the expression is Null, System.NullReferenceException is raised.
The Raise statement without the specified expression can be used only within the exception handler in the Except block of the Try statement. In this case it continues handling (that is, further throwing) the current exception that is being handled at the moment.
When an exception is raised, the control is transferred to the first Except block in one of the Try enclosing statements that can handle this exception. The process executed after raising an exception and before the control is transferred to an appropriate handler is named throwing an exception. Throwing an exception consists of repeating the following steps until an appropriate handler is found. In this description the raising point is location in the program where the exception was raised.
Each Try statement enclosing the raising point is analyzed in the current functional member. For each S statement, starting from the innermost enclosing Try statement and finishing with the outermost, the following steps are executed:
If the statement block in the Try part of the S statement encloses the raising point and S has one or more Except blocks, these blocks are examined to check if they contain an appropriate handler. The first Except block that handles situations of the raised exception class or of its base class is considered to be an appropriate handler. The general-purpose Except block is considered to be an appropriate handler for any exception. If a matching handler is found, the control is transferred to its statement block.
Otherwise, the statement block in the Try or Except part of the S statement encloses the raising point and the S statement contains the Finally block. The control is transferred to its statement block. If a new exception is raised, while it is executed, handling the current exception is stopped. Otherwise, after finishing execution of the Finally statement block handling the current exception is continued.
If no appropriate exception handler is found in the current functional member, its invocation is interrupted. The steps listed above are repeated for enclosing calls in call stack, the statement, in which the internal functional member was called, is considered to be a raising point.
If the entire call chain is analyzed as a result of exception handling, which means absence of an appropriate handler, execution of the current flow is aborted. Influence of such an exception depends on the corresponding implementation.
Private Sub TestRaise();
Begin
Try
//Error generation
Raise New Exception("Execution error!");
Except On Ex: Exception Do
//Error processing
Finally
End Try;
End Sub;
See also: