Course contentsShow
← React
Lesson 1289 of 2,39633. Error BoundariesFree lesson

What Error Boundaries DON'T Catch: Event Handlers

Errors thrown inside event handlers (onClick, onChange, etc.) are not caught by error boundaries; use try-catch instead.

What Error Boundaries DON'T Catch: Event Handlers

You'll learn why error boundaries have a critical blind spot when it comes to event handlers, and how to handle errors in those cases instead.

Error boundaries are powerful for catching errors during rendering, but they have an important limitation: they don't catch errors thrown inside event handlers like onClick, onChange, onSubmit, and others.

Why Event Handlers Are Different

When React renders your component tree, error boundaries can intercept errors and display fallback UI. But event handlers run after rendering is complete—they're triggered by user interactions, not by React's rendering process. Since they execute outside the render phase, error boundaries don't "see" errors thrown inside them.

Think of it like a safety net under a trapeze. The net catches performers during their act (rendering), but if they walk off stage and trip in the hallway (event handler), the net isn't there anymore.

The Solution: try-catch

For errors in event handlers, you need old-fashioned try-catch blocks:

function MyComponent() {
  const handleClick = () => {
    try {
      // Risky operation
      processData();
    } catch (error) {
      // Handle the error yourself
      console.error('Button click failed:', error);
      // Maybe set error state to show UI feedback
    }
  };

  return <button onClick={handleClick}>Process</button>;
}

If you throw an error in an event handler without catching it, it will crash the app (in development) or log to the console (in production), but your error boundary won't catch it.

Key Takeaway: Error boundaries only catch errors during rendering, lifecycle methods, and constructor—not in event handlers. Use try-catch blocks to handle errors in onClick, onChange, and other event handlers.