PY
JS

Python to JavaScript

11 lessons

Progress0%
1Variables and Constants2Functions and Closures3Lists vs Arrays4Dicts vs Objects and Map5Classes and Prototypes6Modules and Imports7Async Programming8DOM and Browser APIs9Type Hints vs TypeScript10Error Handling11Build Tools and npm
All Mirror Courses
PY
JS
Error Handling
MirrorLesson 10 of 11
Lesson 10

Error Handling

try/catch vs try/except, custom errors, and error patterns

Introduction

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

Mirror Card
PY
From Python:

In Python, you're familiar with try/catch vs try/except, custom errors, and error patterns.

JS
In JavaScript:

JavaScript has its own approach to try/catch vs try/except, custom errors, and error patterns, which we'll explore step by step.

The JavaScript Way

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

JS
JavaScript Example
// try / catch / finally (no 'else' in JS)
try {
  const result = riskyOperation();
  console.log("Success:", result); // in try block
} catch (e) {
  if (e instanceof TypeError) {
    console.error("Type error:", e.message);
  } else if (e instanceof RangeError) {
    console.error("Range error:", e.message);
  } else {
    console.error("Unexpected:", e);
    throw e; // re-throw
  }
} finally {
  cleanup(); // always runs
}

// Custom error class
class InsufficientFundsError extends Error {
  constructor(amount, balance) {
    super(`Need ${amount}, have ${balance}`);
    this.name = "InsufficientFundsError";
    this.amount = amount;
    this.balance = balance;
  }
}

// Throw
throw new InsufficientFundsError(100, 50);

// Async errors
async function fetchData() {
  try {
    const res = await fetch("/api/data");
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return await res.json();
  } catch (e) {
    console.error("Fetch failed:", e);
    throw e;
  }
}

Comparing to Python

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

PY
Python (What you know)
# try / except / else / finally
try:
    result = risky_operation()
except ValueError as e:
    print(f"Bad value: {e}")
except (TypeError, KeyError) as e:
    print(f"Type/Key error: {e}")
except Exception as e:
    print(f"Unexpected: {e}")
    raise  # re-raise
else:
    print("Success:", result)   # runs if no exception
finally:
    cleanup()                   # always runs

# Custom exception
class InsufficientFundsError(ValueError):
    def __init__(self, amount, balance):
        super().__init__(f"Need {amount}, have {balance}")
        self.amount = amount
        self.balance = balance

# Raise
raise InsufficientFundsError(100, 50)

# Context manager (cleanup)
with open("file.txt") as f:
    data = f.read()
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Python has 'else' clause (runs when no exception); JS does not

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Python 'except ExcType as e' vs JS 'catch (e)' — JS catches all types in one block

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

JS uses instanceof inside catch for type discrimination

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Custom errors: both extend base class; JS requires setting this.name manually

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Python's context manager (with) auto-closes; JS uses try/finally for cleanup

Step-by-Step Breakdown

1. Basic try/catch

Python can have multiple except clauses for different types. JS has one catch block — use instanceof to check type.

PY
Python
try:
    x = risky()
except ValueError:
    handle_value_error()
JS
JavaScript
try {
  const x = risky();
} catch (e) {
  if (e instanceof RangeError) handleRangeError(e);
  else throw e; // re-throw unknown errors
}

2. Custom Error Classes

Both languages support custom exception hierarchies. In JS, set this.name so error messages show the class name.

PY
Python
class MyError(ValueError):
    def __init__(self, msg):
        super().__init__(msg)
JS
JavaScript
class MyError extends Error {
  constructor(msg) {
    super(msg);
    this.name = "MyError"; // important!
  }
}
Common Pitfall
Without this.name = 'MyError', console.log shows 'Error:' not 'MyError:' in stack traces.

3. Re-throwing Errors

Both languages let you re-throw errors to propagate them up the call stack after logging or partial handling.

PY
Python
except Exception as e:
    log(e)
    raise  # re-raise same exception
JS
JavaScript
} catch (e) {
  log(e);
  throw e; // re-throw — not throw new Error(e)
}
Common Pitfall
In JS, 'throw new Error(e)' creates a NEW error and loses the original stack trace. Use 'throw e' to re-throw.

4. Async Error Handling

async/await uses try/catch for async errors in both languages. Always handle rejections — unhandled rejections crash the process.

PY
Python
async def fetch():
    try:
        return await client.get(url)
    except httpx.HTTPError as e:
        raise RuntimeError(f"fetch failed: {e}")
JS
JavaScript
async function fetch() {
  try {
    const res = await axios.get(url);
    return res.data;
  } catch (e) {
    throw new Error(`fetch failed: ${e.message}`);
  }
}

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python has 'else' clause (runs when no exception); JS does not
  • Python 'except ExcType as e' vs JS 'catch (e)' — JS catches all types in one block
  • JS uses instanceof inside catch for type discrimination
Common Pitfall
Don't assume JavaScript works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Python: except Type as e for each type; JS: one catch + instanceof checks
  • Python has 'else' (success path) — in JS, put success code at end of try block
  • Custom errors: extend Error; in JS set this.name for readable stack traces
  • Re-throw with 'throw e' not 'throw new Error(e)' to preserve stack trace
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in JavaScript to practice these concepts.
PreviousNext