
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
416
|
Chapter 7: Exception Handling
See Also
See the “Exception Handling (Debugging)” topic in the MSDN documentation.
7.16 Preventing the Nefarious
TypeInitializationException
Problem
Problems can occur when initializing a class’s or a structure’s static fields. Some of
these problems are serious enough to raise a
TypeInitializationException excep-
tion. Unfortunately, this exception can be hard to track down and can potentially
shut down your application. You want to prevent this from occurring.
Solution
To demonstrate how to handle a TypeInitializationException, take the following
example that initializes static fields to a value,
null, or does not initialize them at all
(not initializing is not recommended, of course), as is the case with the following
class:
public class TestInit
{
public static object one;
public static string two = one.ToString( );
}
Figure 7-3. The Exceptions dialog box after adding a user-defined exception to the TreeView