--- title: Error Handling description: Learn how to display expected errors and handle uncaught exceptions. related: title: API Reference description: Learn more about the features mentioned in this page by reading the API Reference. links: - app/api-reference/functions/redirect - app/api-reference/file-conventions/error - app/api-reference/functions/not-found - app/api-reference/file-conventions/not-found --- Errors can be divided into two categories: [expected errors](#handling-expected-errors) and [uncaught exceptions](#handling-uncaught-exceptions). This page will walk you through how you can handle these errors in your Next.js application. ## Handling expected errors Expected errors are those that can occur during the normal operation of the application, such as those from [server-side form validation](/docs/app/guides/forms) or failed requests. These errors should be handled explicitly and returned to the client. ### Server Functions You can use the [`useActionState`](https://react.dev/reference/react/useActionState) hook to handle expected errors in [Server Functions](https://react.dev/reference/rsc/server-functions). For these errors, avoid using `try`/`catch` blocks and throw errors. Instead, model expected errors as return values. ```ts filename="app/actions.ts" switcher 'use server' export async function createPost(prevState: any, formData: FormData) { const title = formData.get('title') const content = formData.get('content') const res = await fetch('https://api.vercel.app/posts', { method: 'POST', body: { title, content }, }) const json = await res.json() if (!res.ok) { return { message: 'Failed to create post' } } } ``` ```js filename="app/actions.js" switcher 'use server' export async function createPost(prevState, formData) { const title = formData.get('title') const content = formData.get('content') const res = await fetch('https://api.vercel.app/posts', { method: 'POST', body: { title, content }, }) const json = await res.json() if (!res.ok) { return { message: 'Failed to create post' } } } ``` You can pass your action to the `useActionState` hook and use the returned `state` to display an error message. ```tsx filename="app/ui/form.tsx" highlight={11,19} switcher 'use client' import { useActionState } from 'react' import { createPost } from '@/app/actions' const initialState = { message: '', } export function Form() { const [state, formAction, pending] = useActionState(createPost, initialState) return (