Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

Why Errors Happen

In the course of writing any type of computer program, three types of errors can crop up:

Let's look at how these errors occur and how you can avoid them.

How to Avoid Syntax Errors

Syntax errors occur when you misspell a variable name, object name, or reference an object incorrectly. Syntax errors are the easiest errors to avoid, because Visual Studio .NET lets you know if a keyword or variable is misspelled. If you're coding in Visual Basic, you can set the Option Explicit option on in your Project Properties dialog box, which tells Visual Studio .NET to make sure that all variables you use in your code are declared before they are referenced. This feature isn't available in C#, but you're notified of syntax errors in both C# and Visual Basic .NET by squiggly lines that appear under code that isn't correct after you type a line of code. When a squiggly line appears under your code, you can hover your mouse over the squiggly line and a tooltip lets you know what error is occurring. Figure 7.1 demonstrates this behavior in C# and Figure 7.2 demonstrates this behavior in Visual Basic .NET.

07fig01.jpg

Figure 7.1 Visual Studio .NET syntax error notification in C#.

07fig02.jpg

Figure 7.2 Visual Studio .NET syntax error notification in Visual Basic .NET.

As you can see, it's almost impossible to have syntax errors when using Visual Studio .NET. The IDE always lets you know when something's wrong with the code you just typed. At the other end of the spectrum are logic errors, which can be undetectable.

How to Avoid Logic Errors

Logic errors are the hardest errors to avoid. Even code written by the most seasoned programmers can contain logic errors. A logic error is a flaw in the way you designed a function, or group of functions, to perform a specific task. Logic errors aren't normally discovered until an application is out in production and being used by end users. This types of error is unavoidable. The only real way to minimize logic errors is to write your applications based on specifications that have been well thought out and clearly defined.

How to Avoid Runtime Errors

Runtime errors are errors that occur at runtime, but aren't handled by an error handler in your code. Runtime errors are the worst type of error that can occur because they directly affect the end user of your application. Runtime errors can also be called laziness errors, because they can be completely avoided simply by implementing some sort of error handling in your code. When an unexpected error occurs and there's no error-handling routine in your application, the application notifies the end user of the error with a very offensive message and crashes immediately. Figure 7.3 shows an example of a runtime error occurring.

07fig03.jpg

Figure 7.3 The offensive runtime error notification to the end user.

In the code that caused the dialog box shown in Figure 7.3, there was no error handler or any kind of check for the existence of a file when the application attempted to use the File.Copy method, so the application aborted. Errors like this are unacceptable, and cause you hours of grief if they aren't avoided, which is why understanding exceptions in .NET and how your application can handle them is so important.

Share ThisShare This

Informit Network