The Raise statement throws an exception.
raise-statement:
Raise expressionopt
The Raise statement with the specified expression throws 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 thrown.
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 thrown, the control is passed to the first Except block in one of the Try enclosing statements that can handle this exception. The process executed after throwing an exception and before the control is passed 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 throwing point is location in the program where the exception was thrown.
Each Try statement enclosing the throwing 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 throwing 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 thrown exception class or of its basic 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 passed to its statement block.
Otherwise, the statement block in the Try or Except part of the S statement encloses the throwing point and the S statement contains the Finally block. The control is passed to its statement block. If a new exception is thrown, 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 throwing 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 handling
Finally
End Try;
End Sub;
See also: