
Handling errors gracefully prevents a single component crash from taking down the entire application. Blazor 6+ introduced Error Boundaries for this purpose.
<ErrorBoundary>
<ChildContent>
<WeatherWidget />
</ChildContent>
<ErrorContent Context="ex">
<p class="error">Failed: @ex.Message</p>
</ErrorContent>
</ErrorBoundary>protected override async Task OnInitializedAsync()
{
try
{
data = await Http.GetFromJsonAsync<List<Item>>("/api/items") ?? new();
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}Blazor WASM shows a built-in error bar at the bottom when unhandled exceptions occur. Customize this in wwwroot/index.html by styling the #blazor-error-ui element.
Inject ILogger<T> and call _logger.LogError(ex, "message") for structured error logging.
Reference:
TaskLoco™ — The Sticky Note GOAT