www.gibmonks.com

Main Page

  Previous Section Next Section

15.4 Methods Reference

GetBaseException

Exception = HttpException.GetBaseException( )

Returns an Exception object representing the original exception in a set of nested exceptions. This property provides a shortcut to the innermost exception accessible via the InnerException property.

Parameter

Exception

An Exception instance that will be populated by the method.

Example

The following code example creates a set of three nested exceptions, the second and third of which are created with an overloaded constructor that sets the InnerException property to the prior exception. The code throws the third exception, which is caught by the Catch statement. The Catch block displays the error message of both the immediate inner exception by using the InnerException property and the original exception by using the Message property of the exception returned by the GetBaseException method:

Sub Page_Load( )
   Try
      Dim myHttpEx As _
         New HttpException("This is the original exception")
      Dim myHttpEx2 As _
         New HttpException("This is a nested exception", myHttpEx)
      Throw New HttpException("Threw an exception from Page_Load", _
         myHttpEx2)
   Catch HttpEx As HttpException
      Dim InnerHttpEx As HttpException
      InnerHttpEx = CType(HttpEx.InnerException, HttpException)
      Message.Text = "ERROR:<br/>"
      Message.Text &= "Message: " & HttpEx.Message & "<br/>"
      Message.Text &= "Inner Exception Message: " & _
         InnerHttpEx.Message & "<br/>"
      Message.Text &= "Base Exception Message: " & _
         InnerHttpEx.GetBaseException.Message & "<br/>"
   End Try
End Sub

Notes

Like the TargetSite property example, this example accesses a property of the instance returned by the GetBaseException method directly, rather than creating a local instance variable first. The same caveats about reduction in code versus readability apply here.

GetHtmlErrorMessage

string = HttpException.GetHtmlErrorMessage( )

Returns a string containing the HTTP error message (if any) set by the originator of the exception.

Parameter

string

A string variable that will receive the value from the method.

Notes

This method will return a value only if the HttpException contains an HTTP error message.

GetHttpCode

integer = HttpException.GetHttpCode( )

Returns an integer containing the HTTP status code contained within the exception. For most exceptions thrown by the ASP.NET intrinsic objects, this integer will be 500, indicating an HTTP server error.

Parameter

integer

An integer variable to receive the HTTP code from the method.

Example

The code example causes an exception by calling Server.Execute on a page that does not exist. The exception is then caught and the HTTP status code is displayed by calling GetHttpCode:

Sub Page_Load( )
   Try
      Server.Execute("Foo.aspx")
   Catch HttpEx As HttpException
      Message.Text = "ERROR:<br/>"
      Message.Text &= "Http Status Code: " & _
         HttpEx.GetHttpCode( ) & "<br/>"
   End Try
End Sub

Notes

This method is most useful for custom exceptions raised in methods that make HTTP calls, since it allows you to pass back the HTTP result code (404 for not found, 403 for access denied, etc.) to the calling client.

ToString

string = HttpException.ToString( )

Returns a string containing the fully qualified name of the exception, the error message (if available), the name of the inner exception, and the stack trace.

Parameter

string

A string variable to receive the value from the method.

Example

The code example causes an exception by attempting to set a Session value on a page for which the EnableSessionState attribute of the @ Page directive has been set to False. The example code then displays the string representation of the resulting exception:

<%@ Page Language="vb" EnableSessionState="false" %>
  
Sub Page_Load( )
   Try
      Session("foo") = "Foo"
   Catch HttpEx As HttpException
      Message.Text = "ERROR:<br/>"
      Message.Text &= "String Representation of Exception: " & _
         HttpEx.ToString( ) & "<br/>"
   End Try
End Sub

Notes

This method provides a quick and easy shortcut for displaying the available information about a given exception without having to call the individual methods and properties of the HttpException object.

      Previous Section Next Section