Example

using System;
using System.IO;
using System.Text;
namespace Samples
{
  public class EndOfStreamExceptionSample
  {
    public static void Main()
    {
      string s = "filestream.txt";
      FileStream fs = new FileStream(s, FileMode.Open);
      BinaryReader br =
                 new BinaryReader(fs, Encoding.ASCII );
      try
      {
        while(true)
        {
          char c = br.ReadChar();
        }
      }
      catch(EndOfStreamException e )
      {
        Console.WriteLine("Exception: {0}", e);
      }
    }
  }
}
The output is
Exception: System.IO.EndOfStreamException: Unable to read beyond the end of the stream.
   at System.IO.__Error.EndOfFile()
   at System.IO.BinaryReader.ReadChar()
   at Samples.EndOfStreamExceptionSample.Main() in C:\Books\BCL\Samples\System.IO
\EndOfStreamException\EndOfStreamException.cs:line 19

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.