认识Exception


// Example for the Exception.HelpLink, Exception.Source,
// Exception.StackTrace, and Exception.TargetSite properties.
using System;

namespace NDP_UE_CS
{
    
// Derive an exception; the constructor sets the HelpLink and 
    
// Source properties.
    class LogTableOverflowException : Exception
    {
        
const string overflowMessage = "The log table has overflowed.";

        
public LogTableOverflowException(
            
string auxMessage, Exception inner)
            :
                
base(String.Format("{0} - {1}",
                    overflowMessage, auxMessage), inner)
        {
            
this.HelpLink = "http://msdn.microsoft.com";
            
this.Source = "Exception_Class_Samples";
        }
    }

    
class LogTable
    {
        
public LogTable(int numElements)
        {
            logArea 
= new string[numElements];
            elemInUse 
= 0;
        }

        
protected string[] logArea;
        
protected int elemInUse;

        
// The AddRecord method throws a derived exception if 
        
// the array bounds exception is caught.
        public int AddRecord(string newRecord)
        {
            
try
            {
                logArea[elemInUse] 
= newRecord;
                
return elemInUse++;
            }
            
catch (Exception e)
            {
                
throw new LogTableOverflowException(
                    String.Format(
"Record \"{0}\" was not logged.",
                        newRecord), e);
            }
        }
    }

    
class OverflowDemo
    {
        
// Create a log table and force an overflow.
        public static void Main()
        {
            LogTable log 
= new LogTable(4);

            Console.WriteLine(
                
"This example of \n   Exception.Message, \n" +
                
"   Exception.HelpLink, \n   Exception.Source, \n" +
                
"   Exception.StackTrace, and \n   Exception." +
                
"TargetSite \ngenerates the following output.");

            
try
            {
                
for (int count = 1; ; count++)
                {
                    log.AddRecord(
                        String.Format(
                            
"Log record number {0}", count));
                }
            }
            
catch (Exception ex)
            {
                Console.WriteLine(
"\nMessage ---\n{0}", ex.Message);
                Console.WriteLine(
                    
"\nHelpLink ---\n{0}", ex.HelpLink);
                Console.WriteLine(
"\nSource ---\n{0}", ex.Source);
                Console.WriteLine(
                    
"\nStackTrace ---\n{0}", ex.StackTrace);
                Console.WriteLine(
                    
"\nTargetSite ---\n{0}", ex.TargetSite);
            }
            Console.Read();
        }
    }
}

posted @ 2008-11-26 11:58  roboth  阅读(317)  评论(0编辑  收藏  举报