Page 1 of 1

Error Handling Tutorial This tutorial shows you how to use error handling

#1 theunborncoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 123
  • Joined: 15-October 09

Post icon  Posted 09 December 2009 - 12:26 AM

Most of the time we handle error in try catch way but there are other ways to do this.

we can handle error at various levels :

Page Error occur on page.

Application error occur on global.aspx page.

I am not gonna show try catch way (its knows to every one)

I am gonna show you other ways

Lets start with page level :

Page Error Event : we can write common function to handle all error occur on page. if we have to handle error with some special message then only use try catch or when you expecting some error then only use try catch.
eg. divide by zero error expecting
#Region "PAGE LEVEL ERROR HANDLING"
	Protected Sub Page_Error(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Error
		Dim ex As Exception
		ex = Server.GetLastError() 'GIVE REFERENCE TO LAST ERROR
		Server.ClearError() 'HANDLE EXCEPTION
	End Sub
#End Region



above code handle errors come on single page


We can use common page to show and log all error means any error occur then redirect to this page.

#Region "PAGE LEVEL ERROR HANDLING"
	Protected Sub Page_Error(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Error
		Dim ex As Exception
		ex = Server.GetLastError() 'GIVE REFERENCE TO LAST ERROR
		Session.Add("ErrorToHandle", ex)
	End Sub
#End Region



Now look for Other way that is at Global.asax
Global.asax contain 1 method named as Application_Error.
You can Write your Error handling code in this function.

	public sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
  
		' Code that runs when an unhandled error occurs

	End Sub



Working Of Error Handling:-

if any error happens then it goes to Page Level means it goes to that pages's Page_Error method. If the error is not handled on that page then that error is redirected to Global.asax means it goes to Application_Error method If there is also error is not handled then this error is displayed to user. This is the basic working of error handling.

Redirecting Users to Different Pages Depending on the Error

Use the customErrors section in web.config. You have to create Error Pages.

		<customErrors mode="On" defaultRedirect="showerror.aspx">
			<error statusCode="403" redirect="NoAccess.htm" />
			<error statusCode="404" redirect="pagenotfound.aspx" />
		</customErrors>



mode attribute of customError specifies whether to show user-defined custom error pages or ASP.NET error pages
if mode is On - Custom error pages are always shown
if mode is Off - Custom error pages are not shown. Instead, ASP.NET error pages will be displayed
if mode is RemoteOnly - Custom error pages are shown for all remote users
if the error is a 404 (File Not Found), then you could set the error page as pagenotfound.aspx
If any error occur other than 403 & 404 then showerror.aspx page is displayed.


Thanks....

Is This A Good Question/Topic? 0
  • +

Page 1 of 1