Java

Error Handling in ASP.net

In this article, we will learn Error handling in ASP.net.

ASP.NET uses the common language runtime (CLR) that provides a way of notifying errors of application in a uniform way.

When an error occurs, an exception is thrown. An exception is any error, condition, or unexpected behavior that an application encounters.

An exception is an object that inherits from the System.Exception class. An exception is thrown from an area of code where a problem has occurred.

The exception is passed up the call stack to a place where the application provides code to handle the exception. If the application does not handle the exception, the browser is forced to display the error details.

In an ASP.NET Web Forms application, exceptions can be handled at the following levels:

  • Application level
  • Page level
  • Code level

As a best practice, Handling errors at the code level in Try/Catch/Finally blocks within your code

Exception Class:

The Exception class is the base class from which exceptions inherit. Most exception objects are instances of some derived class of the Exception class, such as the SystemException class, the IndexOutOfRangeException class, or the ArgumentNullException class. The Exception class has properties, such as the StackTrace property, the InnerException property, and the Message property, that provide specific information about the error that has occurred.

Application Level Error Handling:

You can handle default errors at the application level either by modifying your application’s configuration or by adding an Application_Error handler in the Global.asax file of your application.

You can handle default errors and HTTP errors by adding a customErrors section to the Web.config file. The customErrors section allows you to specify a default page that users will be redirected to when an error occurs. It also allows you to specify individual pages for specific status code errors.

when you use the configuration to redirect the user to a different page, you do not have the details of the error that occurred.

you can trap errors that occur anywhere in your application by adding code to the Application_Error handler in the Global.asax file.

Page Level Error Handling:

A page-level handler returns the user to the page where the error occurred.
But instances of controls are not maintained in that page , so there will no longer be anything on the page.

To provide the error details to the user of the application, you must specifically write the error details to the page.

You would use a page-level error handler to log unhandled errors or to take the user to a page that can display helpful information.

This code example shows a handler for the Error event in an ASP.NET Web page.
This handler catches all exceptions that are not already handled within try/catch blocks in the page.

Code Level Error Handling:
Code Level Error Handling
The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception. If the currently executing method does not contain a catch block, the CLR looks at the method that called the current method, and so on, up the call stack. If no catch block is found, then the CLR displays an unhandled exception message to the user and stops execution of the program.

The following code example shows a common way of using try/catch/finally to handle errors.

You can read more detailed on this article:
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

© 2015, www.techkatak.com. All rights reserved.