Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. try / catch / finally

try / catch / finally

The try / catch / finally statement catches exceptions that occur during program execution and handles them safely.

Syntax

// Basic syntax for catching exceptions.
try {
	// Code that may throw an exception
} catch (ExceptionType e) {
	// Code to handle the caught exception
} finally {
	// Code that always runs, regardless of whether an exception occurred
}

Syntax Overview

SyntaxDescription
tryA block that wraps code which may throw an exception.
catch (ExceptionType e)Catches an exception of the specified type. You can chain multiple catch blocks to handle different exception types individually.
catch (Exception e) when (condition)Catches an exception only when the condition expression evaluates to true.
finallyA block that always runs whether or not an exception was thrown. Typically used to release resources.
e.MessageGets the error message string from the exception.
e.StackTraceGets the call stack information at the point the exception was thrown.

Sample Code

using System;

// Basic exception handling.
try {
	int[] arr = new int[3];
	arr[10] = 100; // Index is out of range.
} catch (IndexOutOfRangeException e) {
	Console.WriteLine("Array index out of range: " + e.Message);
}

// Using multiple catch blocks to handle different exception types.
try {
	string input = "abc";
	int number = int.Parse(input); // Throws an exception if conversion fails.
} catch (FormatException e) {
	Console.WriteLine("Format error: " + e.Message);
} catch (OverflowException e) {
	Console.WriteLine("Overflow: " + e.Message);
} catch (Exception e) {
	// Catches all other exceptions (place this last).
	Console.WriteLine("Unexpected error: " + e.Message);
}

// Using finally to ensure cleanup always runs.
System.IO.StreamReader reader = null;
try {
	reader = new System.IO.StreamReader("data.txt");
	Console.WriteLine(reader.ReadToEnd());
} catch (System.IO.FileNotFoundException e) {
	Console.WriteLine("File not found: " + e.Message);
} finally {
	// Close the file regardless of whether an exception occurred.
	reader?.Close();
	Console.WriteLine("finally block executed");
}

// Using a when filter for conditional exception handling.
try {
	throw new Exception("Critical error");
} catch (Exception e) when (e.Message.Contains("Critical")) {
	Console.WriteLine("Critical error detected: " + e.Message);
}

Notes

try / catch / finally is the fundamental exception-handling syntax in C#. Always wrap code that may fail at runtime — such as file I/O, network communication, or type conversion — in a try/catch block. Uncaught exceptions will crash your program.

catch blocks are evaluated from top to bottom, so place specific exception types (such as FormatException) before the general Exception catch. If you put the general catch first, the more specific ones will never be reached.

For how to throw exceptions, see 'throw / Custom Exceptions'.

If you find any errors or copyright issues, please .