Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] - نسخه متنی

Jason Beres

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید









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.


/ 270