codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

27 / 60 topics
27Exceptions in C#28Try-Catch-Finally Blocks29Custom Exceptions in C#
Tutorials/C# Programming/Exceptions in C#
🔷C# Programming

Exceptions in C#

Updated 2026-05-15
10 min read

Exceptions in C#

Introduction

In the world of programming, errors are inevitable. They can occur due to various reasons such as invalid user input, unexpected data formats, or system limitations. Handling these errors gracefully is crucial for building robust applications. In C#, exceptions provide a structured way to handle runtime errors.

Exceptions are events that disrupt the normal flow of a program's execution. When an error occurs, an exception object is created and thrown. The program can then catch this exception and handle it appropriately, preventing the application from crashing.

Concept

C# uses a try-catch-finally block structure for handling exceptions. Here’s how it works:

  1. Try Block: This block contains the code that might throw an exception.
  2. Catch Block: This block catches the exception thrown by the try block and handles it.
  3. Finally Block: This optional block executes regardless of whether an exception was thrown or not, typically used for cleanup activities.

Examples

Basic Try-Catch Block

Let's start with a simple example where we divide two numbers and handle any potential division by zero error.

csharp
1using System;
2
3class Program
4{
5 static void Main()
6 {
7 try
8 {
9 Console.Write("Enter the first number: ");
10 int num1 = Convert.ToInt32(Console.ReadLine());
11
12 Console.Write("Enter the second number: ");
13 int num2 = Convert.ToInt32(Console.ReadLine());
14
15 int result = num1 / num2;
16 Console.WriteLine($"The result is {result}");
17 }
18 catch (DivideByZeroException ex)
19 {
20 Console.WriteLine("Error: Cannot divide by zero.");
21 }
22 }
23}

In this example:

  • The try block contains the code that might throw a DivideByZeroException.
  • The catch block catches the exception and prints an error message.

Multiple Catch Blocks

You can have multiple catch blocks to handle different types of exceptions. Here’s an example:

csharp
1using System;
2
3class Program
4{
5 static void Main()
6 {
7 try
8 {
9 Console.Write("Enter the first number: ");
10 int num1 = Convert.ToInt32(Console.ReadLine());
11
12 Console.Write("Enter the second number: ");
13 int num2 = Convert.ToInt32(Console.ReadLine());
14
15 int result = num1 / num2;
16 Console.WriteLine($"The result is {result}");
17 }
18 catch (DivideByZeroException ex)
19 {
20 Console.WriteLine("Error: Cannot divide by zero.");
21 }
22 catch (FormatException ex)
23 {
24 Console.WriteLine("Error: Invalid input format.");
25 }
26 }
27}

In this example:

  • The first catch block handles DivideByZeroException.
  • The second catch block handles FormatException, which occurs if the user inputs a non-numeric value.

Finally Block

The finally block is used to execute code that should run regardless of whether an exception was thrown or not. This is often used for cleanup activities like closing files or releasing resources.

csharp
1using System;
2
3class Program
4{
5 static void Main()
6 {
7 try
8 {
9 Console.Write("Enter the first number: ");
10 int num1 = Convert.ToInt32(Console.ReadLine());
11
12 Console.Write("Enter the second number: ");
13 int num2 = Convert.ToInt32(Console.ReadLine());
14
15 int result = num1 / num2;
16 Console.WriteLine($"The result is {result}");
17 }
18 catch (DivideByZeroException ex)
19 {
20 Console.WriteLine("Error: Cannot divide by zero.");
21 }
22 finally
23 {
24 Console.WriteLine("Execution completed.");
25 }
26 }
27}

In this example:

  • The finally block executes after the try and catch blocks, regardless of whether an exception was thrown.

What's Next?

In the next section, we will explore more advanced error handling techniques in C#, including custom exceptions and using the throw keyword to rethrow exceptions. Stay tuned!

Info

Understanding how to handle exceptions is crucial for writing reliable and maintainable code. Practice by implementing exception handling in your own projects.


PreviousDelegates in C#Next Try-Catch-Finally Blocks

Recommended Gear

Delegates in C#Try-Catch-Finally Blocks