Error Handling

You can and should avoid bugs, but there are runtime errors that cannot be avoided and must be handled. The simplest bugs to find and fix are syntax errors: violations of the rules of the language. For example, suppose you had the following line of code in your C# program:

intt i;

or this line in your VB.NET program:

dim i as intgr

In either case, when you try to compile the program, you will get a compiler error, because in each case, the keyword for integer is misspelled.

Syntax errors are reduced dramatically when using Visual Studio .NET. Depending on how VS.NET is configured, any code element that isn’t recognized is underlined. If Auto List Members is turned on, the incidence of syntax errors is further reduced. Finally, because VB.NET doesn’t necessarily require explicit variable declaration, you can turn Option Explicit on to eliminate typos as a source of syntax errors.

Should any syntax errors remain, or if you are using a different editor, then any syntax errors will be caught by the compiler every time you try to build the project. It is nearly impossible for a syntax error to slip by into production code.

Tip

When the compiler finds a syntax error, an error message containing the location of the error and a terse explanation will be displayed in the Output window of VS.NET. If the error is caused by something such as an unbalanced parenthesis or bracket (or a missing semicolon in C#), then the actual error may not be on the exact line reported.

More problematic, ...

Get Programming ASP .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.