Working with Exceptions using ASP.NET Ajax | WebReference

Working with Exceptions using ASP.NET Ajax

By Joydip Kanjilal

Exception handling is a technique that enables you to handle runtime errors. This article presents what exceptions are and how they can be handled using ASP.NET Ajax.

What are Exceptions?

Exceptions are errors that occur at runtime and disrupt the normal flow of control in a program. You can handle exceptions in your application's code to ensure that the normal flow of the program is not terminated.

Exception Blocks

Exception blocks are keywords that are used to handle exceptions. Examples of exception blocks include, try, catch, finally. The try block is one that contains the code that can raise an exception at runtime. Exceptions raised inside the try block are handled inside appropriate catch blocks. Note that a try block should contain one or more catch blocks or at least a finally block and that if an exception occurs, only one of the catch blocks will be executed. If an exception doesn't occur, the finally block would be executed. Actually finally blocks are executed irrespective of whether an exception has occurred - these blocks become handy in cleaning up unused or unwanted memory resources, file handles, etc.

Here is how the syntax of a typical try-catch-finally combination looks:

You can have multiple catch blocks but only one finally block for each try block. It should be noted that exceptions should not be used to determine the program flow and should not be overused. Overuse of exception blocks in your application's code can have a detrimental effect on the application's performance. Exceptions should only be used when required, i.e., in exceptional situations.

The following code listing illustrates how a try-catch block can be used to handle exceptions:

Note that when you use a statement like, "throw ex;" in your code, a new exception instance is thrown and the exception stack trace information is lost. Therefore, if you don't want to handle exceptions and just want to propagate it, you can just use the statement "throw" in lieu of statements such as, "throw ex". This would prevent the stack trace information from being lost and the exception would be propagated appropriately. Here is an example:

Handling Exceptions using ASP.NET Ajax

In this section and the sections that follow, we will learn how we can use ASP.NET Ajax to handle errors and exceptions in our application. Consider the following markup code:

The following code snippet shows how you can throw exception explicitly:

Custom Error Handling in ASP.NET AJAX

You can make use of the EndRequestEventArgs class in ASP.NET Ajax to implement custom error handling in your applications. Note that the endRequest event of the Sys.WebForms.PageRequestManager class is triggered immediately after a postback is complete. The following code listing shows how you can generate a divide by zero error explicitly using ASP.NET Ajax:

You can also handle the endRequest event in your code using the add_endRequest method of the Sys.WebForms.PageRequestManager instance and attaching the handler. Here is an example:

The complete source code now looks like the code listing below:

You can also handle exceptions using the events of ScriptManager control. To do this, you need to set the OnAsyncPostBackError handler in the definition of the ScriptManager control in your webform as shown in the code snippet below:

In addition, here's how you can define the OnAsyncPostBackError handler:

Suggested Readings


In this article, we have had a look at how we can handle exceptions using ASP.NET Ajax framework. We have discussed the exception blocks, best practices and how we can handle exceptions efficiently using this framework. Happy reading!