Demonstrates defining and using a custom exception class

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// CustExcp.cs -- Demonstrates defining and using a custom exception class
//
//                Compile this program with the following command line:
//                    C:>csc CustExcp.cs
//
namespace nsCustomException
{
    using System;
    using System.IO;
    
    public class CustExcpclsMain
    {
        static public void Main (string [] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine ("usage: CustExcp FileName String");
                return;
            }
            try
            {
                ReadFile (args&#91;0&#93;, args&#91;1&#93;);
                Console.WriteLine (args&#91;1&#93; + " was not found in " + args&#91;0&#93;);
            }
// Custom exception thrown. Display the information.
            catch (clsException e)
            {
                Console.WriteLine ("string {0} first occurs in {1} at Line {2}, Column {3}",
                                   args&#91;1&#93;, args&#91;0&#93;, e.Line, e.Column);
                Console.WriteLine (e.Found);
                return;
            }
// Check for other possible exceptions.
            catch (ArgumentException)
            {
                Console.WriteLine ("The file name " + args &#91;0&#93; +
                          " is empty or contains an invalid character");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine ("The file name " + args &#91;0&#93; +
                                   " cannot be found");
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine ("The path for " + args &#91;0&#93; +
                                   " is invalid");
            }
            catch (Exception e)
            {
                Console.WriteLine (e);
            }
        }
        static public void ReadFile (string FileName, string Find)
        {
            FileStream strm;
            StreamReader reader;
            try
            {
                strm = new FileStream (FileName, FileMode.Open);
                reader = new StreamReader (strm);
                int Line = 0;
                while (reader.Peek () >= 0)
                {
                    ++Line;
                    string str = reader.ReadLine ();
                    int index = str.IndexOf (Find);
                    if (index >= 0)
                    {
                        reader.Close ();
                        strm.Close ();
                        clsException ex = new clsException ();
                        ex.Line = Line;
                        ex.Column = index + 1;
                        ex.Found = str;
                        throw (ex);
                    }
                }
                reader.Close ();
                strm.Close ();
                return;
            }
            catch (IOException e)
            {
// If file not found, go back and get another name
                if (e is FileNotFoundException)
                    throw (e);
// Code here to handle other IOException classes
                Console.WriteLine (e.Message);
                throw (new IOException());
            }
       }
    }
// Define a class derived from Exception
    class clsException : Exception
    {
        public int Line = 0;
        public int Column = 0;
        public string Found = null;
    }
}