This reminds me Windows 95/97’s blue screen error messages that always used to end on ‘If the problem persists, please contact your application vendor’. Anyways, so that’s it; we are done with simplest form of application level, exception handling. Now as we know, every hero needs a villain; we need a villain page that would throw exceptions. Let’s start coding Default.aspx:

As you can easily guess, as long as texts in txtNum1 and txtNum2 are valid numbers and txtNum2’s text is not zero, application will work fine. But the moment you enter some non numeric text in any of these text boxes or you enter zero in txtNum2, you’ll get an error, see the following screenshots.

Good, divided successfully, but things are not always like that:

Dumb user!

See how simple that was? That’s the end of the first approach, application level exception handling.

Ok now let’s try the second approach, module level error handling; this requires a little bit of coding. Before we start working on the second approach let’s quickly add 3 more lines in Default.aspx; these lines will help us navigation to module level web pages:

Now let’s suppose we have a separate module in our application and we want some different information to be displayed on the error page, in case an exception is uncaught. As we’ve already added a folder named ‘Module’ and we have pages in that folder as well so let’s start coding ModuleForm1.aspx.cs; copy-paste the exact code from Default.aspx. The only difference is, in ModuleForm1.aspx we’ll add one extra directive ErrorPage=”~/Module/ModuleErrorPage.aspx” in the first line of the page:

This will tell the application that if an error occurs in this page, don’t go to main application’s ErrorPage.aspx; go to ModuleErrorPage.aspx in its stead. btnDivide_Click here is a little bit more different as well.

In this case, we are catching the highest level of exception, which is the parent of all the exceptions and after storing it in a session level variable we are re-throwing the same exception. The result would be, as you might have already guessed, application will take you to ModuleErrorPage.aspx, where we have done some coding in slightly different way than ErrorPage.aspx. Here is the body of ModuleErrorPage.aspx:

<body>

<formid=”form1″runat=”server”>

<div>

<b>

Some unexpected error occured in the application. If the problem persists, please contact your application vendor.

In this page we’ve added two new label controls for displaying verbose error message. Page_Load event of ModuleErrorPage will also be different:

protectedvoid Page_Load(object sender, EventArgs e)

{

if (Session[“ModuleException”] != null)

{

Exception exc = (Exception)Session[“ModuleException”];

this.lblErrorMessage.Text = exc.Message;

this.lblErrorDetails.Text = exc.ToString();

}

}

Run the application and on Default page, select Go to Module Form1 button, we’ll get From1, enter bad data in it, the resultant screenshots are as follows:

And we’ll get a lot better error message as this:

That’s the end of the second approach.

Now let’s see the third approach, my favorite one. What I normally do is I try to catch as many exceptions within the form as I can think of. Nonetheless, there are still many occasions where is miss to foresee how unexpected and error producing data, users can enter (in other words, how dumb users can be). Thus I always try to give an informative error message on the same page for all the exceptions I could expect on that page and then there is a standard error message for all other exceptions that were really ‘unexpected’; blend of above two approaches, require a little bit more coding. Let’s see this in action in our ModuelForm2.aspx, but before that let’s add one more entry in web.config:

This whole code will produce exactly the same effect as produced in ModuleErrorPage. Code is pretty easy to understand so I think there is no need of any explanation. Now let’s see ModuleForm2.aspx. UI will be exactly same as Default.aspx, btnDivide_Click, however, will be different as follows:

this.lblResult.Text = “Division by zero is not allowed, please enter another value.”;

}

catch (Exception exc)

{

throw exc;

}

}

In this method, I caught DivideByZeroException and displayed a proper message right in the page. For all other exceptions I just caught them and re-threw them. All such exceptions will be caught in Global.asax’s Application_Error method. There, an error message will be displayed depending upon web.config’s settings. If this is a development server then surely you want ‘Verbose’ error message. On production server, nevertheless, you should keep it ‘Succinct’. Clients don’t like to watch long red error message which they don’t even understand. The above code will engender the following screenshots.

Diving by zero? Not a problem, I knew user would do this…

Exception caught right at the spot. But what if user enters something I couldn’t even think of?

Don’t worry, for this villain, I have my Global.asax hero standing by:

On production server, keep web.config’s entry as this:

<addkey=“ErrorMessage“value=“Succinct“ />

Which will show the following screen on the same error:

So that’s it folks; exception handling in .NET, simple and robust. Feel free to give me your feedback, should you have any questions, don’t hasitate in asking, not even for the source code.

Until next time…

Advertisements

Like this:

Hei Mukarram,
3rd method is very interesting.I tried 3rd method.but i have a doubt that in which page we get the error message?.
we are generating error message from Global.asax.But how can we display it(using which page)?.It will be appreciable,If you could send the code or can you help me.
Thanks a lot for the post

As you can see in browser’s address bar, the page will be the same where error was generated. The code in the global.asax will clear the contents of the current page and display the error message in it. Once error is displayed, Back button will take you back to the original form of the page.

For source code, I’ll try to find this project’s source and will send it to you. As you can see no one ever asked for this project’s source, you are the first one. That’s why I almost lost its project files, but let me see if I can find it.