Error Handling

Errors are not signs of failure — they are signals. Learn everything from basic try/catch to advanced real-world error architectures used in production apps.

Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

💡 Running Code Locally: While this online editor runs real JavaScript, some advanced examples may have limitations. For the best experience:

Why Error Handling Matters

JavaScript gives you extremely powerful tools to handle, detect, filter, categorize, and recover from errors — synchronously AND asynchronously.

1. What Is an Error in JavaScript?

An error occurs when JavaScript cannot complete an operation. Examples:

By default, errors stop execution. Your job is to catch, interpret, and recover from them.

2. Try...Catch — The Foundation

3. Throwing Your Own Errors

JavaScript allows you to create and throw your own errors:

4. Built-In Error Types

5. Using Error Names to Handle Smartly

This lets you create fine-grained error logic.

6. Error Handling in Asynchronous Code

7. Re-Throwing Errors

Sometimes you want to catch an error, log it, then pass it upward:

8. Creating Custom Error Classes

9. Nested Try...Catch Blocks

10. Best Practices for Error Handling

11. Real-World Example — Payment Processing

12. Real-World Example — Robust API Fetching

Now your UI doesn't crash when the API fails — it gracefully recovers.

13. Real-World Example — Form Validation

14. Advanced Async Error Handling

Asynchronous code is where 90% of real production errors happen.

You MUST master async error strategies to build real-world websites, apps, and SaaS systems.

15. Fetch Errors Aren't Always "Errors"

⚠️ It does NOT throw errors for bad HTTP codes (404, 500, 403, 429)

Without this check, your code will behave like everything is fine when it absolutely isn't.

16. Retry Logic with Exponential Backoff

Real-world apps must recover from errors gracefully:

Retries at: 200ms → 400ms → 600ms. This is used by Google, AWS, Netflix, Stripe, PayPal.

17. Defensive Programming

Defensive programming = writing code that assumes things will go wrong.

18. Global Error Handling

Most frameworks implement a global error handler:

19. Production-Grade Error Handler

20. Common JavaScript Error Scenarios

You will encounter these daily in real development.

🎯 Practice Projects for Mastery

Build a function: safeFetch(url, retries = 3)

📋 Quick Reference — Error Handling

Method

Purpose

try...catch

Catch errors in a block of code

throw

Manually trigger an error

finally

Run code regardless of outcome

Error Object

Has .message and .stack

Custom Error

class MyError extends Error

Lesson 10 Complete — Error Handling!

You've mastered the art of writing robust code that handles failure gracefully. This separates amateur code from professional software.

Up next: Closures & Scope — diving deep into advanced JavaScript concepts! 🧠

Practice quiz

In a try/catch/finally block, when does the finally block run?

  • Only when an error is thrown
  • Only when no error is thrown
  • Always, regardless of the outcome
  • Never, it is optional

Answer: Always, regardless of the outcome. finally always runs, making it ideal for cleanup like closing connections or removing loaders.

Which keyword do you use to manually trigger an error?

  • throw
  • raise
  • error
  • catch

Answer: throw. throw manually triggers an error, for example throw new Error('Cannot divide by zero').

Which error type is thrown by null.toUpperCase()?

  • ReferenceError
  • SyntaxError
  • RangeError
  • TypeError

Answer: TypeError. Calling a method on null is an incorrect data type operation, which throws a TypeError.

Which error type occurs when you use a variable that was never declared?

  • TypeError
  • ReferenceError
  • RangeError
  • URIError

Answer: ReferenceError. Referencing an undeclared variable throws a ReferenceError.

How do you create a custom error class?

  • class MyError extends Error { }
  • function MyError()
  • new CustomError()
  • Error.create('My')

Answer: class MyError extends Error { }. Custom errors extend the built-in Error class, e.g. class ValidationError extends Error.

Does fetch() throw an error for HTTP status codes like 404 or 500?

  • Yes, always
  • Only for 500
  • No, you must check res.ok yourself
  • Only in strict mode

Answer: No, you must check res.ok yourself. fetch only rejects for network-level failures; you must check res.ok for bad HTTP status codes.

What does 're-throwing' an error mean?

  • Catching an error and ignoring it
  • Catching an error, handling it locally, then throwing it again to pass it upward
  • Throwing two errors at once
  • Converting an error to a string

Answer: Catching an error, handling it locally, then throwing it again to pass it upward. Re-throwing lets you log or add context locally, then pass the error up with throw err.

How can you handle errors in async/await code?

  • With a .then() only
  • Errors cannot be caught in async code
  • With window.onerror only
  • By wrapping the awaited calls in try/catch

Answer: By wrapping the awaited calls in try/catch. async/await uses ordinary try/catch around the awaited operations to catch failures.

In an outer/inner nested try/catch, where is an error thrown in the inner try caught first?

  • The outer catch
  • The inner catch
  • Both simultaneously
  • Neither

Answer: The inner catch. The nearest enclosing catch handles it first; the inner catch catches the inner error.

Which is a recommended error-handling best practice from the lesson?

  • Show database stack traces to users
  • Ignore async errors
  • Use specific error messages and don't leak sensitive info
  • Fail silently

Answer: Use specific error messages and don't leak sensitive info. Use clear, specific messages, handle async errors, and never leak sensitive details to users.

Continue this course