6.4 Creating and Throwing Custom Exceptions

 <  Day Day Up  >  

You want to create a new exception type that is more informative and applicable to an object within the application that throws it.


Technique

Create a new class derived from System.ApplicationException . Override any applicable properties to customize the exception information, such as Message and HelpLink , as shown in Listing 6.4.

To throw a custom or predefined exception, use the keyword throw followed by an instance of the exception you want to throw.

Listing 6.4 Creating a Custom Exception Class
 using System; using System.Collections; namespace _4_CustomException {     class Class1     {         [STAThread]         static void Main(string[] args)         {             OddIntCollection oddValues = new OddIntCollection();             int input = 0;             do             {                 // get input from user                 Console.Write( "Enter an odd number or -1 to quit: " );                 input = Int32.Parse( Console.ReadLine() );                 // add it to the collection                 if( input != -1 )                     oddValues.Add( input );             } while( input != -1 );         }     }     class OddIntCollection : CollectionBase     {         public void Add( int value )         {             // check if value is odd by using a logical & on first bit             if( (value & 0x1) == 0 )             {                 // value isn't odd, throw the custom exception                 throw new OddIntException();             }             else             {                 // add value to inner collection                 InnerList.Add( value );             }         }     }     class OddIntException : ApplicationException     {         // override Message property to display custom message         public override string Message         {             get             {                 return "Value is not an odd number";             }         }     } } 

Comments

There are two classifications of exceptions. The most common exception is a system exception, which is any exception derived from the System.SystemException base class. There are 70 direct descendants of System.SystemException defined within the .NET Framework, each of which may have 0 or more derived classes themselves . Some of these exceptions include arithmetic exceptions such as DivideByZeroException , I/O exceptions such FileNotFoundException , and argument exceptions such as ArgumentOutOfRangeException .

Even though there are many available system exceptions that you are free to throw within your application as necessary, you might need to eventually create one of your own. Creating a custom exception is simply a matter of creating a new class derived from ApplicationException and overriding a few key properties. The most important property is the string Message , which allows you to return a readable string detailing why the exception occurred. In Listing 6.4, the OddIntCollection will only accept odd integers. If a user attempts to place an even integer, an OddIntException is thrown.

The Add method of the OddIntCollection class first checks the integer parameter to see whether it's odd. It does so by performing a simple bit check on the least significant bit. If that bit is 1, then you know the number is odd. When an even number is encountered , an OddIntException is thrown by using the C# keyword throw followed by the creation of an OddIntException object. If the exception is not handled, the CLR accesses the Message and StackTrace property of the exception object and displays that information within the console, if the application is running within the console. If a developer has created an exception handler, she might or might not access those properties.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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