Exception实例

///<summary>
/// Exception Sample
///</summary>

namespace ExceptionSample
{
    
using System;
    
class EntryPoint
    
{
        
static void Main()
        
{
            
string userInput;
            
while (true)
            
{
                
try
                
{
                    Console.WriteLine(
"Input a number between 1 and 5 and press enter to exit");
                    userInput 
= Console.ReadLine();
                    userInput 
= userInput.ToString().Trim();
                    
if (userInput == "")
                        
break///exit while loop
                    int index = Convert.ToInt32(userInput);//maybe throw FormatException
                    if (index < 0 || index > 5)
                        
throw new System.IndexOutOfRangeException("you typed in" + userInput);///Throw IndexOutOfRangeException to cacth
                    Console.WriteLine("your number is  " + index);
                }

                
catch (System.FormatException e)
                
{
                    Console.WriteLine(
"FormatException:Number should be between 1  and  5" + e.Message);
                }

                
catch (System.IndexOutOfRangeException e)
                
{
                    Console.WriteLine(
"IndexOutOfRangeException:Number should be between 1  and  5" + e.Message);
                }

                
catch (System.Exception e)
                
{
                    Console.WriteLine(
"some other exception occured"+e.Message);
                }

                
finally
                
{
                    Console.WriteLine(
"thank you ");
                }

            }

        }

    }

}


实例二
///<summary>
/// Exception Sample
///</summary>

namespace ZYG.CSharp.Exception

    
using System;
    
using System.IO;
    
///<summary>
    
/// defined exception class
    
///</summary>

    class LandLineSpyFoundException : System.ApplicationException
    
{
        
public LandLineSpyFoundException(string spyName)
            : 
base("LandLine spy found,with name=" + spyName)
        

            
        }

        
public LandLineSpyFoundException(string spyName, Exception innerException)
            : 
base("LandLine spy found,with name=" + spyName, innerException)
        

            
        }

    }

    
/////////////////////////////////////////////////
    class ColdCallFileFormatException : System.ApplicationException
    
{
        
public ColdCallFileFormatException(string message)
            : 
base(message)
        
{

        }

        
public ColdCallFileFormatException(string message, Exception innerException)
            : 
base( message,innerException)
        
{

        }

    }

    
////////////////////////////////////
    class UnexpectedException : System.ApplicationException
    
{
        
public UnexpectedException(string message)
            : 
base(message)
        
{

        }

        
public UnexpectedException(string message, Exception innerException)
            : 
base(message, innerException)
        
{

        }

    }


    
///<summary>
    
/// file reader class
    
///</summary>

    class ColdCallFileReader : System.IDisposable
    
{
        FileStream fs;
        StreamReader sr;
        
uint nPeopleToRing;
        
bool isDisposed = false;
        
bool isOpen = false;

     
        
public void Open(string filename)
        
{
            
if (isDisposed)
                
throw new System.ObjectDisposedException("peopleToRing");//-------------------Exception Point
            fs = new System.IO.FileStream(filename, FileMode.Open);        //FileNotFoundException Point
            sr=new System.IO.StreamReader(fs);
            
try
            
{
                
string firstLine = sr.ReadLine(); //人员个数
                nPeopleToRing = uint.Parse(firstLine);             ///Exception Point FomatException
                isOpen=true;
            }

            
catch( System.FormatException e)
            
{
                
throw new ColdCallFileFormatException("firstLine isn\'t an integer", e);
            }

            
finally
            
{}
           
        }


        
public void ProcessNextPerson()
        
{

            
if (isDisposed)
                
throw new System.ObjectDisposedException("peopleToRing");//-------------------Exception Point
            if (!isOpen)
                
throw new UnexpectedException("Attempt to access cold call first is not open");
            
try
            
{
                
string name;
                name 
= sr.ReadLine();
                
if (name == null)
                    
throw new ColdCallFileFormatException("Not enough names");
                
if (name[0== 'Z')
                    
throw new LandLineSpyFoundException(name);
                Console.WriteLine(name);
            }

            
catch (LandLineSpyFoundException e)
            
{
                Console.WriteLine(e.Message);
            }

            
finally
            
{ }
        }

        
public uint NPeopleToRing
        
{
            
get {

                
if (isDisposed)
                    
throw new System.ObjectDisposedException("peopleToRing");//-------------------Exception Point
                if (!isOpen)
                    
throw new UnexpectedException("Attempt to access cold call first is not open");

                
return nPeopleToRing;
            }

        }

        
public void Dispose()
        
{
            
if (isDisposed)
                
return;
            isDisposed 
= true;
            isOpen 
= false;
            
if (fs != null)
            
{
                fs.Close();
                fs 
= null;
            }


        }

    }



    
class EntryPoint
    
{
        
static void Main()
        
{
            
string filename;
            Console.WriteLine(
"Please type in the name of the file containing the names of the people to be cold-Called");
            filename 
= Console.ReadLine();
            ColdCallFileReader peopleToRing 
= new ColdCallFileReader();
            
try
            
{
                peopleToRing.Open(filename);
                
for (int i = 0; i < peopleToRing.NPeopleToRing; i++)
                
{
                    peopleToRing.ProcessNextPerson();

                }

                Console.WriteLine(
"All callers processed correctly");
            }

            
catch (System.IO.FileNotFoundException e)
            
{
                Console.WriteLine(
"The file{0} does not exist", filename);
            }

            
catch (ColdCallFileFormatException e)
            
{
                Console.WriteLine(
"The file {0} appears have been corrupted", filename);
            }

            
catch (System.Exception e)
            
{
                Console.WriteLine(
"Exception occured :\n"+e.Message);
            }

            
finally
            
{
                peopleToRing.Dispose();
            }

            Console.ReadLine();

        }

    }


}
posted @ 2007-04-27 21:06  roboth  阅读(477)  评论(0编辑  收藏  举报