Demonstrates using if statements to sort out an IOException : Exception Try Catch « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Language Basics » Exception Try CatchScreenshots 
Demonstrates using if statements to sort out an IOException

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

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// IOExcept.cs -- Demonstrates using if statements to sort out an IOException
//
//                Compile this program with the following command line:
//                    C:>csc IOExcept.cs
//
namespace nsExcept
{
    using System;
    using System.IO;
    public class IOExcept
    {
        static public void Main (string [] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine ("Please enter a file name");
                return;
            }
            ReadFile (args[0]);
        }
        static public void ReadFile (string FileName)
        {
            FileStream strm = null;
            StreamReader reader = null;
            try
            {
                strm = new FileStream (FileName, FileMode.Open,
                                                   FileAccess.Read);
                reader = new StreamReader (strm);
                while (reader.Peek() 0)
                {
                    string str = reader.ReadLine();
                    Console.WriteLine (str);
                }
            }
            catch (IOException e)
            {
                if (e is EndOfStreamException)
                {
                    Console.WriteLine ("Attempted to read beyond end of file");
                }
                else if (e is FileNotFoundException)
                {
                    Console.WriteLine ("The file name " + FileName +
                                       " cannot be found");
                    return;
                }
                else if (e is DirectoryNotFoundException)
                {
                    Console.WriteLine ("The path for name " + FileName +
                                       " cannot be found");
                    return;
                }
                else if (e is FileLoadException)
                {
                    Console.WriteLine ("Cannot read from " + FileName);
                }
                reader.Close();
                strm.Close ();
            }
            catch (Exception e)
            {
                Console.WriteLine (e.Message);
            }
        }
    }
}



           
       
Related examples in the same category
1.Catch Error
2.illustrates an unhandled exceptionillustrates an unhandled exception
3.illustrates how to handle a specific exceptionillustrates how to handle a specific exception
4.illustrates multiple catch blocksillustrates multiple catch blocks
5.illustrates a nested try/catch blockillustrates a nested try/catch block
6.illustrates exception propagation with methodsillustrates exception propagation with methods
7.Catch Divide By Zero ExceptionCatch Divide By Zero Exception
8.Demonstrates stacking catch blocks to provide alternate code for more than one exception type
9.Catches an exception that was thrown in a component
10.Throw a format exception purposely to demonstrate catching a FormatException
11.Several catch branchesSeveral catch branches
12.Demonstrate exception handlingDemonstrate exception handling
13.An exception can be generated by one method and caught by anotherAn exception can be generated by one     method and caught by another
14.Exception Type MismatchException Type Mismatch
15.Handle error gracefully and continueHandle error gracefully and continue
16.Use multiple catch statementsUse multiple catch statements
17.Use the 'catch all' catch statementUse the 'catch all' catch statement
18.Use a nested try blockUse a nested try block
19.Catch different exceptions
20.Passing Exceptions on to the Caller: Caller Confuse
21.Passing Exceptions on to the Caller: Caller Inform
22.Exception Handling:Trying and CatchingException Handling:Trying and Catching
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.