Try / Catch Basic : A try block with a StreamReader statement that opens a data file called data.txt and writes a string from the file. Following the try block is a catch block that catches any exception that results from the try block.
// This will throw a 'System.IO.FileNotFoundException'
public class ProcessFile
{
public static void Main()
{
try
{
StreamReader sr = File.OpenText("data.txt");
Console.WriteLine("The first line of this file is {0}", sr.ReadLine());
sr.Close();
}
catch (Exception e)
{
Console.WriteLine("An error occurred: '{0}'", e);
}
}
}