Error Handling
An unhandled exception can break a whole Interactive Server circuit. Error boundaries contain the damage.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 29, 2026
Related tutorials
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#
<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;
}
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#
- ErrorBoundary wraps only the risky component.
- ErrorContent replaces it when an exception occurs.
- Recover() resets the boundary and renders the child again.
- 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#
Users lose everything on any error. Wrap independent areas separately.
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
<ErrorBoundary>
<ChildContent><WeatherWidget /></ChildContent>
<ErrorContent><p>Weather unavailable</p></ErrorContent>
</ErrorBoundary>
Mini quiz#
What does ErrorBoundary.Recover() do?
Want more? Practise all Blazor MCQs · Blazor interview questions
My notes#
Saved in this browser.