In many cases, you might want some form of centralized exception handling rather than surrounding every piece of code with Try Catch blocks. You also might want some way to log and handle unhandled exceptions and forward the end user to an error page which has the same look and feel of your site collection rather than the silly error page shipped with SharePoint. In point of fact, there is nothing worse than an error being thrown at the face of the end user and you cannot figure out what caused the problem because the exception is unhandled and it is not revealed anywhere in the log files.
A great way for centralized logging, exception handling and providing a friendly error page is to employ a global exception handler at the application level. This will catch any unhandled exceptions, log them, clear them and redirect the user to a fully branded page. You can also extend your global exception handler to send notification mails to the site collection administrator informing him/her that an unhandled exception has occurred.
The way you can implement global exception handling in regular ASP.NET applications is via the Global.asax file which can enclose code that responds to application-level events. These are events that are raised at specific points during the running of the asp.net application. One of those events is the Application_Error event which fires whenever an unhandled exception occurs in the application; the next code snippet demonstrates the Application_Error event.
protected void Application_Error(object sender, EventArgs e)
// Code that runs when an unhandled error occurs
Exception unhandledException = Server.GetLastError().GetBaseException();
Commence the Windows Explorer and navigate to the physical root directory of your SharePoint web application, there you should see the global.asax file. SharePoint automatically adds this file every time you create a new SharePoint web application. If you open this file in Visual Studio, you should see the following:
Figure 1. Global.asax file for SharePoint web applications.
As you see, the global.asax file contains two directives. The @Assembly directive references the Microsoft.SharePoint.dll assembly, which contains the ApplicationRuntime namespace. Note that the Inherits attribute of the @Application directive instructs ASP.NET to use SPHttpApplication as the base class for the class that it dynamically creates and instantiates. Let’s use the .NET Reflector to investigate this class; the next figure demonstrates this in action. If you don’t know what .NET Reflector is, refer to the “Troubleshooting Toolbox” section at the end of the issue.
Figure 2. Using .NET Reflector to investigate SharePoint Global.asax.
As you might have concluded, you cannot trap unhandled exceptions through Global.asax; this place has been taken by the product itself. Alternatively, you need to hook into the ASP.NET HTTP pipeline by creating an HTTP Module. The next figure shows a sample HTTP Module that you can start with to create your own global exception handler.
Figure 3. Using HTTP Module to globally handle and log unhandled exceptions.