Efcore: Document exceptions that should not be caught as part of normal flow control (i.e. most of them)

Created on 25 Feb 2020  Â·  4Comments  Â·  Source: dotnet/efcore

Asynchronously returns the only element of a sequence, and throws an InvalidOperationException exception if there is not exactly one element in the sequence.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

closed-duplicate customer-reported

Most helpful comment

Team notes: after discussing this we think it would be more helpful to:

  • Document exceptions that are intended to be caught for some scenarios. (Basically, just DbUpdateConcurrencyException.)
  • Document that other exceptions (like this one) are intended to indicate that the application is doing something wrong and therefore should not be caught other than as part of reporting/logging the application fault.

All 4 comments

@linhub15 We can document the type of exception thrown, but I'm curious as to why it matters? It is not intended that this exception be expected and caught as a normal part of an application running. It is intended to indicate that there isn't a single element and therefore calling this method was an invalid operation.

I wasn't sure if other Exceptions would bubble up from SingleAsync() so
Rather than:

catch (Exception e) { ... } 

I wanted to use:

catch (InvalidOperationException e) { ... } 

So I could throw my custom Exception EntityNotFoundException


I think I used this method incorrectly...

In hind sight I should have used SingleDefaultOrAsync to check for default, then throw my Exception.

Team notes: after discussing this we think it would be more helpful to:

  • Document exceptions that are intended to be caught for some scenarios. (Basically, just DbUpdateConcurrencyException.)
  • Document that other exceptions (like this one) are intended to indicate that the application is doing something wrong and therefore should not be caught other than as part of reporting/logging the application fault.

Duplicate of #17692

Was this page helpful?
0 / 5 - 0 ratings