Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre The Flow of try-catch-finally | Fundamentals of Exception Handling
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Exceptions and Error Handling Practice

bookThe Flow of try-catch-finally

When you work with resources like files, network connections, or database connections, you need to make sure those resources are properly released, no matter what happens in your code. This is where the finally block comes into play. In C#, a finally block is used with try and catch to guarantee that certain code will always run, whether or not an exception is thrown. Its main role is to help you manage resources safely and avoid leaving things open or in an inconsistent state.

Note
Definition

Resource cleanup refers to the process of releasing resources such as files, network connections, or memory that your program has acquired during execution. The finally block is preferred for cleanup because it always runs, even if an exception is thrown, while catch only runs when an exception occurs.

FileStreamExample.cs

FileStreamExample.cs

copy
123456789101112131415161718192021222324252627282930
using System; using System.IO; namespace ConsoleApp { public class FileStreamExample { public void ReadFile() { FileStream fileStream = null; try { fileStream = new FileStream("data.txt", FileMode.Open); // Read from the file } catch (FileNotFoundException) { Console.WriteLine("File not found."); } finally { if (fileStream != null) { fileStream.Close(); // Always executed, even if an exception occurs } } } } }

If an exception is thrown inside the try block, such as when the file does not exist, the program will jump to the catch block. However, regardless of whether an exception was thrown or not, the finally block will always execute. This ensures that any cleanup code, like closing a file or releasing a resource, is run every time.

NestedTryCatchFinally.cs

NestedTryCatchFinally.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { var example = new NestedTryCatchFinally(); example.Process(); } } public class NestedTryCatchFinally { public void Process() { try { Console.WriteLine("Outer try start"); try { Console.WriteLine("Inner try start"); // Possible exception } catch { Console.WriteLine("Inner catch"); } finally { Console.WriteLine("Inner finally"); } } catch { Console.WriteLine("Outer catch"); } finally { Console.WriteLine("Outer finally"); } } } }

1. When does the finally block execute in a try-catch-finally statement?

2. What is the main use case for the finally block?

question mark

When does the finally block execute in a try-catch-finally statement?

Select the correct answer

question mark

What is the main use case for the finally block?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 5

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookThe Flow of try-catch-finally

Glissez pour afficher le menu

When you work with resources like files, network connections, or database connections, you need to make sure those resources are properly released, no matter what happens in your code. This is where the finally block comes into play. In C#, a finally block is used with try and catch to guarantee that certain code will always run, whether or not an exception is thrown. Its main role is to help you manage resources safely and avoid leaving things open or in an inconsistent state.

Note
Definition

Resource cleanup refers to the process of releasing resources such as files, network connections, or memory that your program has acquired during execution. The finally block is preferred for cleanup because it always runs, even if an exception is thrown, while catch only runs when an exception occurs.

FileStreamExample.cs

FileStreamExample.cs

copy
123456789101112131415161718192021222324252627282930
using System; using System.IO; namespace ConsoleApp { public class FileStreamExample { public void ReadFile() { FileStream fileStream = null; try { fileStream = new FileStream("data.txt", FileMode.Open); // Read from the file } catch (FileNotFoundException) { Console.WriteLine("File not found."); } finally { if (fileStream != null) { fileStream.Close(); // Always executed, even if an exception occurs } } } } }

If an exception is thrown inside the try block, such as when the file does not exist, the program will jump to the catch block. However, regardless of whether an exception was thrown or not, the finally block will always execute. This ensures that any cleanup code, like closing a file or releasing a resource, is run every time.

NestedTryCatchFinally.cs

NestedTryCatchFinally.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { var example = new NestedTryCatchFinally(); example.Process(); } } public class NestedTryCatchFinally { public void Process() { try { Console.WriteLine("Outer try start"); try { Console.WriteLine("Inner try start"); // Possible exception } catch { Console.WriteLine("Inner catch"); } finally { Console.WriteLine("Inner finally"); } } catch { Console.WriteLine("Outer catch"); } finally { Console.WriteLine("Outer finally"); } } } }

1. When does the finally block execute in a try-catch-finally statement?

2. What is the main use case for the finally block?

question mark

When does the finally block execute in a try-catch-finally statement?

Select the correct answer

question mark

What is the main use case for the finally block?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 5
some-alt