question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error boundaries are not catching all exceptions

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When method is using async void construction Error boundaries are not able to catch exception inside such method and application will crash.

For example: If I have a component wrapped with error boundaries and it has such methods inside it, so this code will lead to a crash.

protected override async Task OnAfterRenderAsync(bool firstRender)
{
        await base.OnAfterRenderAsync(firstRender);

        ThrowError();
}

private async void ThrowError()
{
        await Task.Delay(100);

        throw new Exception("Some exception");
}

Is there a way how to prevent app from crashing in this case? Can you add something, so error boundaries will be able to catch exceptions in async void methods?

Expected Behavior

Crash not happening with error boundaries even if async void method had thrown an exception.

Steps To Reproduce

No response

Exceptions (if any)

Error: System.Exception: Some exception at TestNet6.Components.ComponentWithError.ThrowError() in C:\Data\Blazor\TestNet6\TestNet6\Components\ComponentWithError.razor:line 47 at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.ExecuteSynchronously(TaskCompletionSource`1 completion, SendOrPostCallback d, Object state) at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<.cctor>b__23_0(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) — End of stack trace from previous location — at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.ExecuteBackground(WorkItem item)

.NET Version

6.0.101

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
guardrexcommented, Feb 16, 2022

Is the sync context section the right spot then, or should I create a section for this remark?

## Asynchronous methods (`async`) don’t support returning `void`

The Blazor framework doesn’t track `void`-returning asynchronous methods (`async`). As a result, exceptions aren’t caught if `void` is returned. Always return a <xref:System.Threading.Tasks.Task> from asynchronous methods.

0reactions
javiercncommented, Feb 16, 2022

I wouldn’t mention the synchronization context, just mention that the framework doesn’t track void returning async methods and as a result exceptions will not be caught in those scenarios and they need to change the return type to return a Task

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Error Boundaries not working with React
You can't catch compile-time errors, the Error Boundaries are for run-time errors within the UI. Refer to Compile time vs run time errors....
Read more >
Error Boundaries
An error boundary can't catch an error within itself. If an error boundary fails trying to render the error message, the error will...
Read more >
React error handling with react-error-boundary
It's important to note that error boundaries catch errors during rendering, in lifecycle methods, and constructors of the whole tree below them.
Read more >
React Error Boundaries | Complete Guide
A complete guide to implementing React Error Boundaries, and how to use a third-party tool for handling more sophisticated scenarios.
Read more >
Catching Errors in React with Error Boundaries
Error handling is a must for every application, Error Boundaries are the React-way of dealing with errors within your apps.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found