Tuesday, January 6, 2009

Error Handling in asp.net


The Error handling model in asp.net lets you handle errors at 3 different levels

Method Level
Page Level
Application Level


Method Level:

We use Method level error handling for all the recoverable errors.
For non recoverable errors it goes to next level.

For the all the errors which can be handled we use

Try…Catch blocks

For throwing any message if an error occurred or any exception we use .

Throw… block

To clean up the error we use

Finally… block


Syntax:

Try

‘’..code for execution

Catch ex as Exception

‘’..handle the error

Finally

‘clear the error

End try



Try block contains the code need to be implemented ,
Catch block is used to handle the error
Finally block used to clear the error.


Let us suppose we are using database connection in the code and if the error occurs in the query .net framework doesn’t close the database connections. I this case we need to write the code in the FINALLY block to close he connection.


Page Level:

We use Page Level event handling when we want to trap the errors occurred in the page and redirect the user to another page which the displays the error details.

Implementation:

Write required code in the Page_Error event handler of the page.

The Page_Error event of the ASP.NET Page object is raised any time an unhandled error occurs in a page


Use GetLastError method to get reference to the latest error occurred


Set the ErrorPage property of the page object to the URL of the page which need to display the error details

The page will be redirected to the url mentioned when any unhandled error occurs.


Add error number,message to the query string in the URL.
Dim lastError As Exception

'get the last error that occurred

lastError = Server.GetLastError( )
Page.ErrorPage = "Errorpage.aspx"?errMessage=" & lastError.Message &errnumber=" & lastError.number

  • When any Unhandled error occurred in the page level ,Page_Error event is triggered and the page will be redirected to the error page mentioned with all the details of the error in the query string.
  • If we don’t add any Query string parameters,.NET will assign one for usi.e aspxerrorpath which gives relative path of the error message.

Application Level: If we want to Log all the errors raised in an application in Single location we use application level error handling. Add Page_Error event handler to the page to throw the error to next level i.e application level Add Application_error event handler in global.asax file to perform logging and redirection
To process errors at the application level, each page must include the Page_Error event handler with a single line of code to rethrow the last exception that occurred.

Private Sub Page_Error(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Error 'Rethrow the last error that occurred

Throw Server.GetLastError( )

End Sub

***********Error handling in Application_error

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

Const EVENT_LOG_NAME As String = "Application"

Dim lastException As Exception

Dim Log As EventLog

Dim message As String

'get the last error that occurred

lastException = Server.GetLastError( )

'create the error message from the message in the last exception along 'with a complete dump of all of the inner exceptions (all exception 'data in the linked list of exceptions)

message = lastException.Message & lastException.ToString( )

'Insert error information into the event log

Log = New EventLog

Log.Source = EVENT_LOG_NAME

Log.WriteEntry(message,EventLogEntryType.Error)

'perform other notifications, etc. here

'clear the error and redirect to the page used to display the 'error information

Server.ClearError( )

Response.Redirect("Errorpage.aspx?errMessage=" & lastException.Message _)

End Sub

















No comments:

Post a Comment