Blazor · Production

Error Handling

An unhandled exception can break a whole Interactive Server circuit. Error boundaries contain the damage.

Advanced 5 min Lesson 26 of 31Examples target .NET 9 Blazor Web App.
Before you start:Razor Components
0/31 in Blazor
On this page · 5 min · Advanced

Learning objectives#

  • Wrap risky UI in ErrorBoundary
  • Let users recover
  • Log errors with details
  • Handle expected errors explicitly

Explanation#

Expected problems (network failure, not found, validation) should be handled with try/catch and friendly messages.

Unexpected exceptions inside an <ErrorBoundary> show its ErrorContent instead of crashing the app. Call Recover() to try again.

Unhandled errors in Interactive Server terminate the circuit and show the #blazor-error-ui bar. Log them and keep the message generic.

Code example: A recoverable error boundary#

Razor / MarkupComponents/Pages/Dashboard.razor Illustrative
<ErrorBoundary @ref="boundary">
    <ChildContent>
        <SalesChart />
    </ChildContent>
    <ErrorContent Context="ex">
        <div class="alert" role="alert">
            <p>The sales chart could not be displayed.</p>
            <button @onclick="() => boundary?.Recover()">Try again</button>
        </div>
    </ErrorContent>
</ErrorBoundary>


<RecentOrders />


@code {
    private ErrorBoundary? boundary;
}
Expected output
If SalesChart throws, only that panel shows “The sales chart could not be displayed.” with Try again; RecentOrders keeps working.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. ErrorBoundary wraps only the risky component.
  2. ErrorContent replaces it when an exception occurs.
  3. Recover() resets the boundary and renders the child again.
  4. Other parts of the page are unaffected.

Real-world use case#

Dashboards with many widgets: one failing widget should not take down the whole page.

Common mistakes#

One ErrorBoundary around the whole app

Users lose everything on any error. Wrap independent areas separately.

Showing exception messages to users

They may leak internals. Log details; show a friendly message.

Best practices#

  • Handle expected errors explicitly
  • ErrorBoundary per independent widget
  • Log exceptions with ILogger
  • Customise the reconnect and error UI

Remember this#

Expected → try/catch. Unexpected → ErrorBoundary.

Recover() = try again.

Log details, show friendly text.

Practice exercise#

Your task: Wrap a <WeatherWidget /> so failures show “Weather unavailable”.

Show solution
Razor / Markup Illustrative
<ErrorBoundary>
    <ChildContent><WeatherWidget /></ChildContent>
    <ErrorContent><p>Weather unavailable</p></ErrorContent>
</ErrorBoundary>

Mini quiz#

Error Handling quizMultiple choice Question 1 of 1

What does ErrorBoundary.Recover() do?

Score so far: 0/0

Want more? Practise all Blazor MCQs · Blazor interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.