Uncaught exceptions are unexpected errors that indicate bugs or issues that should not occur during the normal flow of your application. These should be handled by throwing errors, which will then be caught by error boundaries.
Nested error boundaries
Next.js uses error boundaries to handle uncaught exceptions. Error boundaries catch errors in their child components and display a fallback UI instead of the component tree that crashed.
Create an error boundary by adding an error.js file inside a route segment and exporting a React component:
app/dashboard/error.tsx
'use client' // Error boundaries must be Client Components import { useEffect } from 'react' export default function ErrorPage({ error, retry, }: { error: unknown retry: () => void }) { useEffect(() => { // Log the error to an error reporting service console.error(error) }, [error]) return ( <div> <h2>Something went wrong!</h2> <button onClick={ // Attempt to recover by re-fetching and re-rendering the segment () => retry() } > Try again </button> </div> ) } Errors will bubble up to the nearest parent error boundary. This allows for granular error handling by placing error.tsx files at different levels in the route hierarchy.
For component-level error recovery, the catchError function lets you create error boundaries that can wrap any part of your component tree:
app/custom-error-boundary.tsx
'use client' import { catchError, type ErrorInfo } from 'next/error' function ErrorFallback(props: { title: string }, { error, retry }: ErrorInfo) { const message = error instanceof Error ? error.message : String(error) return ( <div> <h2>{props.title}</h2> <p>{message}</p> <button onClick={() => retry()}>Try again</button> </div> ) } export default catchError(ErrorFallback) Then use the returned component as a wrapper in any layout or page:
app/some-component.tsx
'use client' import { catchError, type ErrorInfo } from 'next/error' function ErrorFallback(props: { title: string }, { error, retry }: ErrorInfo) { const message = error instanceof Error ? error.message : String(error) return ( <div> <h2>{props.title}</h2> <p>{message}</p> <button onClick={() => retry()}>Try again</button> </div> ) } export default catchError(ErrorFallback) Error boundaries don't catch errors inside event handlers. They're designed to catch errors during rendering to show a fallback UI instead of crashing the whole app.
In general, errors in event handlers or async code aren’t handled by error boundaries because they run after rendering.
To handle these cases, catch the error manually and store it using useState or useReducer, then update the UI to inform the user.
'use client' import { useState } from 'react' export function Button() { const [error, setError] = useState(null) const handleClick = () => { try { // do some work that might fail throw new Error('Exception') } catch (reason) { setError(reason) } } if (error) { /* render fallback UI */ } return ( <button type="button" onClick={handleClick}> Click me </button> ) } Note that unhandled errors inside startTransition from useTransition, will bubble up to the nearest error boundary.
'use client' import { useTransition } from 'react' export function Button() { const [pending, startTransition] = useTransition() const handleClick = () => startTransition(() => { throw new Error('Exception') }) return ( <button type="button" onClick={handleClick}> Click me </button> ) } 
