Pages

Tuesday, March 16, 2010

How to Catch Unhandled Exceptions in ASP.NET Using Global.asax

Most of the time asp.net developers faces some exceptions in their applications / websites which came in some exceptional and rare cases. To handle them, we will do something new by using global.asax

Also add a new class file under the App_Code folder. In my case, my file name is Myglobal.vb, inherit this class file to HttpApplication.

Now, open the file and select the page event Error in MyGlobal.vb class file and write your code to handle the exception whether you want to log the error or redirect to the customized error page or whatever do you want, it is upto you.

Now add a global.asax file into your project, make sure the Application_OnError event should be written in global.asax file.

Open the file and add a page directive into global.asax like this i.e.

<%@ Application Language="VB" Inherits="MyGlobal" %>

MyGlobal is the name of my class file which i added into App_Code folder earlier.

Test your code.

No comments:

Post a Comment