- Understanding Exceptions
- Handling Exceptions
- Managing Unhandled Exceptions
- Validating User Input
- Practice Questions
- Need to Know More?
Managing Unhandled Exceptions
The AppDomain class represents the application domain, which is an isolated environment where the CLR-managed applications execute. This class generates an event named UnhandledException whenever an unhandled exception occurs in an application. Unhandled exception can occur if there are no try-catch-finally blocks in the code, or if the existing catch blocks in the code cannot catch the thrown exception.
The UnhandledException event handler receives unhandled exception details through its argument, an object of type UnhandledExceptionEventArgs.
Table 3.2 details the most important properties of the UnhandledExceptionEventArgs class.
Table 3.2 Important Properties of the UnhandledExceptionEventArgs Class
Property |
Description |
ExceptionObject |
Receives the unhandled exception object corresponding to the current domain |
IsTerminating |
Is a Boolean value that indicates whether the CLR is terminating |
An event handler can be attached to the UnhandledException event to take custom actions such as logging the exception-related information. A log maintained over a period can help you to analyze and identify patterns providing useful debugging information. Several techniques can be used to log event-related information:
By using the Windows event log
By using the custom log files
By using the databases such as SQL Server 2000
By sending email notifications
The Windows event log provides the most dependable method of logging because other forms of logging can suffer from loss of network connectivity or file access. You can use the EventLog class to write data to the Windows event logs available in Windows 2000 and later versions of the operating system.
TIP
The option to use the EventLog class is not available in older versions of the Windows operating system, such as Windows 98. It should not be used if the application will be deployed on legacy platforms.