This solution demonstrates two more advanced exception mangement techniques.
This sample contains two projects: a Windows Forms client and a Class Library.
Class Library
The class library defines one application class, Customer. In addition it exposes a set of custom exception class which are used to expose application defined errors. Unlike previous versions of Visual Basic (and COM which it was based upon), .NET Exceptions are fully typed removing the need for error numbers. While you can still have error numbers, it is much better to defined application exceptions which can expose a much richer array of information to the caller. In this application there is a hierarchy of exceptions. At the top is CRMSystemException for the fictitious SomeCompany CRM system. It inherits from System.Application exception (as opposed to System.SystemException which is used by Microsoft). From there, two classes inherit from it:
There is nothing in this sample related to employees, but we wanted to show how you can have a larger product that could be made up of various modules or components. There then two more exceptions which both inherit from CustomerException:
Each class exposes different levels of information and functionality. Examine the source code for more comments. Note that your exceptions classes can be named anything, but by convention they should end with the suffix Exception.
Client
The client application has a reference to the class library. Two command buttons show how to execute methods against the customer class and catch the custom exceptions. A third command button causes an unhandled exception. In addition, there is a check box labeled 'Turn on Global Exception Trap' which when checked enabled a global exception trap. When enabled, the code defined in OnThreadException will be executed instead of the default Windows Form handler. Please see the comments for more information.
Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
The program is best run outside the debugger to see the effects of a global exception handler. Run the program and click all three buttons. Note how Edit Customer and Delete Customer display nice error dialogs. Note that by default if you click the third button labeled Untrapped Local Error, a custom dialog appears. This dialog is the global exception handler defined by Windows Forms. Check the check box labeled 'Turn on Global Exception Trap' to see a custom trap in action.