Why Errors Happen
In the course of writing any type of computer program, three types of errors can crop up:
- Syntax errors
- Logic errors
- Runtime errors
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.
Figure 7.1. Visual Studio .NET syntax error notification in C#.

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

Note
In Visual Basic .NET, setting Option Explicit to ON forces you to declare your variables before using them. Option Explicit is ON by default for new projects. The Option Strict setting, which is OFF by default, allows narrowing data conversions to take place without a compile error. You should always have Option Explicit and Option Strict ON to avoid any unexpected errors.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.Tip
To set your project properties for Option Explicit and Option Strict, right-click your project name in the Solution Explorer and select Properties from the contextual menu. When the property page for your project pops up, select the Build node under the Common Properties node, and you'll see the Option Explicit and Option Strict options.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.
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.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.