Error Handling at Page level or Application level

Resolved-error
Resolved-error @Image courtesy of Stuart Miles/ FreeDigitalPhotos.net

Error Handling at Page level or Application level

Welcome to CodeSpread!

A regular query of a developer as which is the best way to handle errors? at Page level or Application level.

Best answer is why to chose one over the other? Lets have both. We will try some sample for Page level Error Handling and Application level Error Handling.

How to Handle errors at Page level?

To catch errors, the best way available is Try/Catch block on code file.

public void Calculate()
{
try
{
//Do some Calculation
}
catch(Exception ex)
{
//Log Exception
}
}

In the above sample, an exception can be corrected at the same time it occurred.

We have another way where a page-level error handler can be created to catch any exceptions that are not caught within Try/Catch block.

private void Page_Error(object sender, EventArgs e)
{
// Get last error
Exception ex = Server.GetLastError();

if (ex is InvalidOperationException)
{
// Handle or Log exceptions
}

}

Note:

  1. If the handler can’t resolve the exception,control is transferred to the error handler in the Global.asax file and is handled at application level.
  2. Still the exception remains unresolved, Web.config file can be configured to redirect the site to redirect to the generic page specified. Refer Custom Errors.

How to Handle errors at Application level?

To Handle errors at Application level,create an error handler in the Global.asax file.

void Application_Error(object sender, EventArgs e)
{
// Get the last error.
Exception ex = Server.GetLastError();

// Handle HTTP errors
if (ex.GetType() == typeof(HttpException))
{
//Handler Implementation

//Redirect to Error page
Server.Transfer("Error.aspx");
}

// Clear the error from the server
Server.ClearError();
}

Global.asax file cannot be used to show error information so server.transfer is used to maintain current context and transfer it to another webpage like an error page. Here, the error information can be shown.

Note: Avoid showing compromising information about the Web site directly to the user.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Custom error mode in ASP.NET web.configCustom error mode in ASP.NET web.config Dual Roles As a developer, I would love to see the error page that provides details of the error, status code and also the stack information. This information is useful to debug the application ...
  • How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010? 1. Open VS2010. 2. Select File -> New Project. 3. Select Visual C# -> Web -> ASP.NET MVC 2 Empty Web Application. Name it as MyMvcApplicat...
  • Don’t Mess with App_OfflineDon’t Mess with App_OfflineOne of the most useful features of ASP.NET is the "App_Offline.htm" . It provides a much easy way to make the ASP.NET application offline while we do our modifications on the site. How we do it? ...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Caching is an evilCaching is an evilI know 'Caching' was created to ease out the network resources but i have seen the situations where it emerged as the biggest evil. I can remember many situations when the website is updated but...
  • C#: Useful JSON in .NetC#: Useful JSON in .NetWhy JSON? All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from databas...
  • 20 Dec: Must Read Codes [6-10]20 Dec: Must Read Codes [6-10]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...