Name

AddError

Synopsis

Context.AddError(ByVal ErrorInfo As Exception)

Adds an Exception object to the array of exceptions returned by the AllErrors property.

Parameters

ErrorInfo

An Exception object to be added to the array.

Example

The example shows how you can use AddError to add an error to the current request context. The exception is created within a Try block. In the Finally block, the ToString method is used to display the error and the ClearError method is used to clear the error so that the page will display properly.

Sub Page_Load(  )
    Try
        Context.AddError(New Exception("Test"))
    Finally
        Message.Text = "Context.Error.ToString(  ) is " & _
           Context.Error.ToString(  )
        Context.ClearError(  )
    End Try
End Sub

Notes

Adding an exception by using AddError should not be confused with throwing an exception. Using AddError only adds the exception to the array returned by the AllErrors property and does not invoke any of the error handling mechanisms in the application.

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.