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

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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









Understanding Exceptions in .NET


When an error occurs in a Visual Basic 6.0 application, the Err object fills with information about the last error that occurred. Your application is then thrown that error information, and the error is handled by the On Error code you wrote. In Visual Basic .NET, the Err object still exists, but for backward compatibility only. In C#, the Err object doesn't exist. To make your code compliant with the Common Language Specification (CLS), you must use the exception object from the System.Exception class in the Framework Class Library (FCL).

The Exception object contains the information you need about the last error that occurred in your application. When an error occurs, an Exception object is created. So, when a block of code causes an error, an exception is thrown and the first block of code in the stack that has an exception handler takes care of that exception. If there's no exception-handling code, a runtime error occurs, and your application terminates. Table 7.1 lists the properties of the Exception object.


































Table 7.1. Properties and Descriptions of the Exception Object

Property Name


Property Description


HelpLink


Gets or sets the help file associated with the application


InnerException


Gets a reference to the inner exception


Message


Gets a string representing the error message associated with the exception


Source


Gets or sets the name of the application object that caused the exception


StackTrace


Gets the stack trace identifying the location in the code where the exception occurred


TargetSite


Gets the method name where the exception occurred

Using the properties of the Exception object, you can determine what to do about the exception that occurred. This might be notifying the user with a MessageBox prompt or handling the error and continuing execution in your code.

Note

Although the information in the Exception object might seem similar to information in the Err object from Visual Basic 6, there are major differences. Exceptions can cross process and machine boundaries. Exceptions are reported the same way in all applications, no matter what language the code is written in or what operating system the code is running on. Finally, exceptions can be traced to the exact line of offending code. Visual Basic 6 offers none of these benefits.

Now that you understand what an exception is, you need to learn how to handle them in code. In .NET, you use structured exception handling (SEH) to help you handle errors in code.

Note

Throughout the day, I refer to exceptions and errors. These words might seem to equate to the same meaning, but in reality an error causes an exception to be thrown. When an error occurs, the Exception object in the common language runtime is filled with information about the error, and your code determines how to handle the exception.


/ 270