Thuta Learning
C#
AdvancedProgrammingBeginner

Exceptions

C#Lesson 2325 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Exceptions
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Exceptions, in plain language

There's always a chance something goes wrong in a program — a missing file, a network failure, a bad array index, invalid user input, you name it. Exception handling lets you handle errors safely so the whole app doesn't crash.

Problem → Why → Solution

The problem — why this concept matters

An empty catch block or `throw ex` hides failure or destroys the original stack trace.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

Exceptions represent exceptional failure flow rather than every expected validation branch. Catch specific exceptions, log or rethrow with context, use `using` or `finally` for cleanup, and preserve stack traces.

Mental model
  1. input or fallible work
  2. validate, execute, and dispose
  3. value or exception

Explain the core concept and runtime behavior of Exceptions

Core syntax or pattern

csharp
try
{
    int[] numbers = { 1, 2, 3 };
    Console.WriteLine(numbers[10]);
}
catch (IndexOutOfRangeException)
{
    Console.WriteLine("Index is outside the array range.");
}
catch (Exception ex)
{
    Console.WriteLine("Something went wrong.");
    Console.WriteLine(ex.Message);
}
finally
{
    Console.WriteLine("Finished checking the array.");
}

Exceptions represent exceptional failure flow rather than every expected validation branch. Catch specific exceptions, log or rethrow with context, use `using` or `finally` for cleanup, and preserve stack traces.

Read the code step by step

  1. input or fallible worktry

    There's always a chance something goes wrong in a program — a missing file, a network failure, a bad array index, invalid user input, you name it. Exception handling lets you handle errors safely so the whole app doesn't crash.

  2. validate, execute, and disposecatch (Exception ex)

    Exceptions represent exceptional failure flow rather than every expected validation branch. Catch specific exceptions, log or rethrow with context, use `using` or `finally` for cleanup, and preserve stack traces.

  3. value or exception}

    Test a file-reading sample for success, missing file, invalid content, and cleanup, separating user messages from diagnostic detail.

Execution flow
  1. input or fallible work
  2. validate, execute, and dispose
  3. value or exception

Success

Explain the core concept and runtime behavior of Exceptions

Failure

An empty catch block or `throw ex` hides failure or destroys the original stack trace.

There's always a chance something goes wrong in a program — a missing file, a network failure, a bad array index, invalid user input, you name it. Exception handling lets you handle errors safely so the whole app doesn't crash.

csharp
try
{
    int[] numbers = { 1, 2, 3 };
    Console.WriteLine(numbers[10]);
}
catch (IndexOutOfRangeException)
{
    Console.WriteLine("Index is outside the array range.");
}
catch (Exception ex)
{
    Console.WriteLine("Something went wrong.");
    Console.WriteLine(ex.Message);
}
finally
{
    Console.WriteLine("Finished checking the array.");
}

try, catch, finally

  • try block holds the code that might throw an error.
  • catch handles the error when one occurs. It's best to catch specific exceptions first.
  • finally runs whether an error happened or not — handy for cleaning up resources.
You should see
Index is outside the array range. Finished checking the array.

Info

⚠️ Best practice

Don't just swallow errors and pretend they never happened. It's much better to show the user a friendly message while logging the details for developers.

Real-world usage

How to apply it in a real project

Test a file-reading sample for success, missing file, invalid content, and cleanup, separating user messages from diagnostic detail.

Mini challenge

Build it yourself

Test a file-reading sample for success, missing file, invalid content, and cleanup, separating user messages from diagnostic detail.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
try
{
    int[] numbers = { 1, 2, 3 };
    Console.WriteLine(numbers[10]);
}
catch (IndexOutOfRangeException)
{
    Console.WriteLine("Index is outside the array range.");
}
catch (Exception ex)
{
    Console.WriteLine("Something went wrong.");
    Console.WriteLine(ex.Message);
}
finally
{
    Console.WriteLine("Finished checking the array.");
}

Expected: Explain the core concept and runtime behavior of Exceptions

Show hint

Test a file-reading sample for success, missing file, invalid content, and cleanup, separating user messages from diagnostic detail.

Quick quiz 1 / 3

What best describes the core idea of Exceptions?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input or fallible work → validate, execute, and dispose → value or exception
  • Exceptions represent exceptional failure flow rather than every expected validation branch. Catch specific exceptions, log or rethrow with context, use `using` or `finally` for cleanup, and preserve stack traces.
  • An empty catch block or `throw ex` hides failure or destroys the original stack trace.
  • Test a file-reading sample for success, missing file, invalid content, and cleanup, separating user messages from diagnostic detail.

Exceptions and exception handling — Microsoft Learn

Easy traps

  • An empty catch block or `throw ex` hides failure or destroys the original stack trace.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Test a file-reading sample for success, missing file, invalid content, and cleanup, separating user messages from diagnostic detail.

You'll know it worked when: Index is outside the array range. Finished checking the array.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Exceptions | Thuta Learning