JS
C#

JavaScript to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Async/Await4Array Methods → LINQ5Exception Handling6Collections7Generics8Delegates and Events9Records and Pattern Matching10File I/O
All Mirror Courses
JS
C#
Exception Handling
MirrorLesson 5 of 10
Lesson 5

Exception Handling

Error handling patterns

Introduction

In this lesson, you'll learn about exception handling in C#. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with error handling patterns.

C#
In C#:

C# has its own approach to error handling patterns, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
public class AppException : Exception {
  public int Code { get; }
  public AppException(string message, int code)
    : base(message) { Code = code; }
}

try {
  if (user == null) throw new AppException("Not found", 404);
  ProcessUser(user);
} catch (AppException ex) when (ex.Code == 404) {
  Console.WriteLine("Not found: " + ex.Message);
} catch (AppException ex) {
  Console.WriteLine(ex.Code + ": " + ex.Message);
} catch (Exception ex) {
  throw; // rethrow preserving stack trace
} finally {
  Cleanup();
}

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
class AppError extends Error {
  constructor(message, code) {
    super(message);
    this.code = code;
    this.name = "AppError";
  }
}

try {
  if (!user) throw new AppError("Not found", 404);
  processUser(user);
} catch (err) {
  if (err instanceof AppError) {
    console.log(err.code, err.message);
  } else {
    throw err;
  }
} finally {
  cleanup();
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# catch clauses specify types; JS uses instanceof inside catch

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# exception filters (when clause) add conditions to catch

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# 'throw;' (bare) rethrows preserving stack trace; 'throw ex;' resets it

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# Exception has InnerException for wrapping; JS uses error.cause

Step-by-Step Breakdown

1. Typed Catch

C# catch clauses specify the exception type directly, replacing JS's instanceof check inside a generic catch.

JS
JavaScript
catch (err) { if (err instanceof AppError) { ... } }
C#
C#
catch (AppException ex) { ... } // type-specific

2. Exception Filters

The 'when' clause adds a condition to a catch block — only catches if the condition is true.

C#
C#
catch (AppException ex) when (ex.Code == 404) {
  // only runs for 404 errors
}

3. throw vs throw ex

Bare 'throw;' rethrows the current exception preserving the original stack trace. 'throw ex;' resets it to the current location.

Rule of Thumb
Always use bare 'throw;' when rethrowing to preserve the original stack trace.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C# catch clauses specify types; JS uses instanceof inside catch
  • C# exception filters (when clause) add conditions to catch
  • C# 'throw;' (bare) rethrows preserving stack trace; 'throw ex;' resets it
Common Pitfall
Don't assume C# works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Typed catch replaces instanceof checks
  • when clause filters exceptions by condition
  • throw; preserves stack trace; throw ex; resets it
  • Exception wrapping via InnerException
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C# to practice these concepts.
PreviousNext