C#idiomatic C#
how-to-use-the-try-catch-block-to-catch-exceptionsstandard/exceptions

Core library

How to use the try/catch block to catch exceptions

Use the try block to contain statements that might raise or throw an exception. Place statements to handle exceptions in one or more catch blocks.

  • page standard/exceptions/how-to-use-the-try-catch-block-to-catch-exceptions
  • updated 02/06/2019

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

In the following example, a System.IO.StreamReader opens a file called data.txt and retrieves a line from the file. Since the code might throw any of three exceptions, it's placed in a try block. Three catch blocks catch the exceptions and handle them by displaying the results to the console.

sample · csharpdocs/standard/exceptions/snippets/how-to-use-the-try-catch-block-to-catch-exceptions/csharp/catchexception.csSnippet3

csharp
//<snippet3>
using System;
using System.IO;

public class ProcessFile
{
    public static void Main()
    {
        try
        {
            using (StreamReader sr = File.OpenText("data.txt"))
            {
                Console.WriteLine($"The first line of this file is {sr.ReadLine()}");
            }
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine($"The file was not found: '{e}'");
        }
        catch (DirectoryNotFoundException e)
        {
            Console.WriteLine($"The directory was not found: '{e}'");
        }
        catch (IOException e)
        {
            Console.WriteLine($"The file could not be opened: '{e}'");
        }
    }
}
//</snippet3>

sample · vbdocs/standard/exceptions/snippets/how-to-use-the-try-catch-block-to-catch-exceptions/vb/catchexception.vbSnippet3

vb
'<snippet3>
Imports System.IO

Public Class ProcessFile
    Public Shared Sub Main()
        Try
            Using sr As StreamReader = File.OpenText("data.txt")
                Console.WriteLine($"The first line of this file is {sr.ReadLine()}")
            End Using
        Catch e As FileNotFoundException
            Console.WriteLine($"The file was not found: '{e}'")
        Catch e As DirectoryNotFoundException
            Console.WriteLine($"The directory was not found: '{e}'")
        Catch e As IOException
            Console.WriteLine($"The file could not be opened: '{e}'")
        End Try
    End Sub
End Class
'</snippet3>

The Common Language Runtime (CLR) catches exceptions not handled by catch blocks. If an exception is caught by the CLR, one of the following results may occur depending on your CLR configuration:

  • A Debug dialog box appears.
  • The program stops execution and a dialog box with exception information appears.
  • An error prints out to the standard error output stream.

See also