Thursday, September 19, 2024

How to Use the Exception Assistant

This article is an excerpt from the book: Murach’s ASP.NET 2.0 Web Programming with C# 2005.

As you test an ASP.NET application, you may encounter runtime errors that prevent an application from executing. When that happens, an exception is thrown. In many cases, the application anticipates these exceptions and provides code to catch them and process them appropriately. If an exception is not caught, however, the application enters break mode and the Exception Assistant displays a dialog box like the one shown in figure 4-8.

As you can see, the Exception Assistant dialog box indicates the type of exception that occurred and points to the statement that caused the error. In many cases, this information is enough to determine what caused the error and what should be done to correct it. For example, the Exception Assistant dialog box in this figure indicates that an input string value was not in the correct format. The problem was encountered in this line of code for the Order page:

item.Quantity = Convert.ToInt32(txtQuantity.Text);

Based on that information, you can assume that the Text property of the txtQuantity control contains a value that can’t be converted to an integer, since the Quantity property of the item object is declared as an integer. This could happen if the application didn’t check that the user entered an integer value into this control. (To allow this error to occur, I disabled the range validator for the Quantity text box on the Order page.)

Many of the exceptions you’ll encounter will be system exceptions like the one shown here. These exceptions apply to general system operations such as arithmetic operations and the execution of methods. If your applications use ADO.NET, you can also encounter ADO.NET and data provider exceptions. If the connection string for a database is invalid, for example, a data provider exception will occur. And if you try to add a row to a data table with a key that already exists, an ADO.NET error will occur.

In some cases, you won’t be able to determine the cause of an error just by analyzing the information in the Exception Assistant dialog box. Then, to get more information about the possible cause of an exception, you can use the list of troubleshooting tips in this dialog box. The items in this list are links that display additional information in a Help window. You can also use the other links in this dialog box to search for more help online, to display the content of the exception object, and to copy the details of the exception to the clipboard.

If you still can’t determine the cause of an error, you can use the Visual Studio debugger to help you locate the problem. You’ll learn how to do that next.

An Exception Assistant dialog box

Description

    If an uncaught exception occurs in an ASP.NET application, the application enters break mode and the Exception Assistant displays a dialog box like the one shown above.

    The Exception Assistant provides the name and description of the exception, and it points to the statement in the program that caused the error. It also includes a list of troubleshooting tips. You can click on these tips to display additional information in a Help window.

    The information provided by the Exception Assistant is often all you need to determine the cause of an error. If not, you can close this window and then use the debugging techniques presented in this chapter to determine the cause.

    If you continue program execution after an exception occurs, ASP.NET terminates the application and sends a Server Error page to the browser. This page is also displayed if you run an application without debugging. It provides the name of the application, a description of the exception, and the line in the program that caused the error. It also includes a stack trace that indicates the processing that led up to the error.

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark Murdok:

Joel Murach has been writing and editing for more than 10 years. During that time, he sharpened his programming skills as a contract programmer in San Francisco and his instructional skills as a trainer for HarperCollins Publishing. He always brings a vision to his projects that leads to improved effectiveness for his readers.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles