0Pricing
C# Academy · Lesson

try/catch/finally, throw new vs rethrow

Use try/catch/finally for predictable error handling; understand throw new vs rethrow; keep examples small and focused.

try/catch/finally, throw new vs rethrow is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Exceptions overview

Goal: Handle and propagate errors safely.

  • try/catch: handle specific exceptions
  • finally: always runs for cleanup
  • throw new vs rethrow
  • Keep handlers short; do not hide bugs

Specific catch

Catch the most specific exception you expect; keep the handler short and clear.

using System;

public class Program
{
  public static void Main(string[] args)
  {
    Console.WriteLine("Start");
    try
    {
      int x = 10;
      int y = 0; // will cause DivideByZeroException
      Console.WriteLine(x / y);
    }
    catch (DivideByZeroException ex)
    {
      Console.WriteLine("Cannot divide by zero: " + ex.GetType().Name);
    }
    Console.WriteLine("Continue");
  }
}

Catch ordering

Order matters: place specific catches first and a generic Exception catch last as a safety net.

using System;

public class Program
{
  static int ParseAndDivide(string text, int divisor)
  {
    // Throws FormatException or DivideByZeroException
    int value = int.Parse(text);
    return value / divisor;
  }

  public static void Main(string[] args)
  {
    try
    {
      Console.WriteLine(ParseAndDivide("not-a-number", 2));
    }
    catch (FormatException)
    {
      Console.WriteLine("Bad number format");
    }
    catch (Exception ex)
    {
      // Fallback last
      Console.WriteLine("Other error: " + ex.GetType().Name);
    }
  }
}

finally for cleanup

Put cleanup in finally. It runs whether an exception happens or not.

using System;

public class Program
{
  static bool opened = false;

  static void UseFakeResource()
  {
    try
    {
      opened = true;
      Console.WriteLine("Open");
      throw new InvalidOperationException("boom");
    }
    catch (InvalidOperationException)
    {
      Console.WriteLine("Handled");
    }
    finally
    {
      opened = false; // cleanup path
      Console.WriteLine("Close");
    }
  }

  public static void Main(string[] args)
  {
    UseFakeResource();
    Console.WriteLine("Opened? " + opened);
  }
}

Rethrow correctly

Use throw; to rethrow and keep the original stack. throw ex; resets it. Wrap with a new exception only when adding context.

using System;

public class Program
{
  static void BadRethrow()
  {
    try
    {
      Fail();
    }
    catch (Exception ex)
    {
      // Resets stack trace:
      throw ex; // BAD rethrow
    }
  }

  static void GoodRethrow()
  {
    try
    {
      Fail();
    }
    catch
    {
      // Preserves original stack trace:
      throw; // GOOD rethrow
    }
  }

  static void Fail()
  {
    throw new InvalidOperationException("fail");
  }

  public static void Main(string[] args)
  {
    try { BadRethrow(); }
    catch (Exception ex)
    {
      Console.WriteLine("Bad: " + (ex.StackTrace == null ? "no stack" : "stack reset likely"));
    }

    try { GoodRethrow(); }
    catch (Exception ex)
    {
      Console.WriteLine("Good: preserves original stack");
    }
  }
}

Handling guidelines

Guidelines:

  • Catch when you can handle or add context.
  • Otherwise, let it bubble up.
  • Keep handlers small; avoid swallowing exceptions silently.
  • Use finally for cleanup; resource disposal is next lesson.

Rethrow rule

Quick check: Which statement correctly rethrows the current exception while preserving the original stack trace?

Recap

Recap: Use try/catch for specific errors, finally for cleanup, and prefer throw; when rethrowing to keep the original stack.

Frequently asked questions

Is the “try/catch/finally, throw new vs rethrow” lesson free?

Yes — the full text of “try/catch/finally, throw new vs rethrow” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “try/catch/finally, throw new vs rethrow”?

Use try/catch/finally for predictable error handling; understand throw new vs rethrow; keep examples small and focused. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “try/catch/finally, throw new vs rethrow” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. try/catch/finally, throw new vs rethrow
  2. Custom exceptions; error design
  3. IDisposable, using statement
← Back to C# Academy