Understanding the .NET Error Handling Classes


An exception occurs when some major error happens within your application. You may have heard the term General Protection Fault (GPF). This is a result of not handling an exception, resulting a fatal crash of your application. However, don't jump to the conclusion that you should start putting exception-handling code everywhere. An exception should be the last step you perform when all else fails. Your error-handling code should be designed well enough so as to avoid an exception altogether. For instance, if you allocate an area of memory to be accessed through a pointer, you should first check to see whether the memory allocation succeeded rather than trying to dereference the pointer and catching the exception when it occurs. In other words, you should catch errors within your application so that an exception never occurs.

Exception handling and debugging with the .NET Framework is very similar to using the exception classes provided in the MFC library. However, the exception handling in the .NET Framework is more robust and provides even a higher level of detail than the MFC library provides. The .NET Framework is highly protected, using exception handling to guard against errors. However, that also requires you to correctly handle the exceptions generated by the .NET Framework. If you fail to do so, your application will terminate with an unhandled exception when an exception is generated.

Debugging statements in your .NET applications are also different than in MFC. Instead of macros, such as ASSERT, TRACE, and so on, the .NET Framework provides classes that give similar functionality and even expanded capabilities. The .NET Framework also provides a means to turn debugging statements on and off dynamically at runtime.

The classes you'll be primarily interested in include the System::Exception class and its derivatives and the System::Diagnostics::Debug and System::Diagnostics::Trace classes. Additional classes in the System::Diagnostics namespace are also useful and worth looking at. For example, there are classes for performance monitoring, event logs, process management, and so on.


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net