robust example that uses exception handling. - CSharp Custom Type

CSharp examples for Custom Type:try catch finally

Description

robust example that uses exception handling.

Demo Code

using System;/*from   w  w  w  . ja va  2s  .  c  o m*/
using System.IO;
class ListFile
{
   public static void Main(string[] args)
   {
      try
      {
         int i=0;
            FileStream fstr = new FileStream("main.cs", FileMode.Open);
            try
            {
               StreamReader sReader = new StreamReader(fstr);
               string line;
               while ((line = sReader.ReadLine()) != null)
               {
                  i++;
                  Console.WriteLine("{0}:  {1}", i, line);
               }
            }
            catch( Exception e )
            {
               Console.WriteLine("Exception during read/write: {0}\n", e);
            }
            finally
            {
               fstr.Close();
            }
      }
      catch (System.IO.FileNotFoundException)
      {
         Console.WriteLine ("ListFile could not find the file {0}", args[0]);
      }
      catch (Exception e)
      {
         Console.WriteLine("Exception: {0}\n\n", e);
      }
   }
}

Result


Related Tutorials