DbContext.SaveChangesAsync Exception Handling

When scaffolding a new ApiController with asynchronous actions and Entity Framework support in Visual Studio 2013, some methods wrap DbContext.SaveChangesAsync calls in try-catch blocks.

For instance, the Put method,

try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return NotFound(); } throw; }

From msdn.microsoft.com about DbUpdateConcurrencyException,

Exception thrown by DbContext when it was expected that SaveChanges for an entity would result in a database update but in fact no rows in the database were affected.

The DbUpdateConcurrencyException is derived from DbUpdateException and there are a handful of other exceptions that can be thrown from the DbContext.SaveChangesAsync method.

I'm wondering why there are no catch clauses for these other exceptions? Is it for the sake of brevity? Or do they simply not belong at this level in the application?

-------------Problems Reply------------

The Web API scaffolding controller follows REST semantics, so a PUT is effectively an update of a record.

The MSDN message states that the exception is thrown if a record was not updated so in this case it will throw an exception if the record failed to be updated. If the record did not exist, a 404 not found is returned.

In summary, although an exception is thrown by the database, it may be a valid case in the context of a REST service (no update performed because the record did not exist) and not a real error. The other exceptions will be bubbled up to the client.

Category:c# Views:2 Time:2018-04-09

Related post

  • Final managed exception handler in a mixed native/managed executable? 2008-08-12

    I have an MFC application compiled with /clr and I'm trying to implement a final handler for otherwise un-caught managed exceptions. For native exceptions, overriding CWinApp::ProcessWndProcException works. The two events suggested in Jeff's CodeProj

  • Exception handling: Contract vs Exceptional approach 2008-08-25

    I know two approaches to Exception handling, lets have a look at them. Contract approach. When a method does not do what it says it will do in the method header, it will throw an exception. Thus the method "promises" that it will do the operation, an

  • Measuring exception handling overhead in C++ 2008-09-04

    What is the best way to measure exception handling overhead/performance in C++? Please give standalone code samples. I'm targeting Microsoft Visual C++ 2008 and gcc. I need to get results from the following cases: Overhead when there are no try/catch

  • What are the principles guiding your exception handling policy? 2008-09-20

    There is a lot of relativity involved in working with exceptions. Beyond low level APIs where exceptions cover errors raised from hardware and the OS there is a shady area where the programmer decides what constitutes an exception and what is a norma

  • Exception handling using an HttpModule 2008-09-25

    We're reviewing one of the company's system's exception handling and found a couple of interesting things. Most of the code blocks (if not all of them) are inside a try/catch block, and inside the catch block a new BaseApplicationException is being t

  • Generic logging of function parameters in exception handling 2008-09-25

    A lot of my C# code follows this pattern: void foo(string param1, string param2, string param3) { try { // do something... } catch(Exception ex) { LogError(String.Format("Error in foo(param1={0}, param2={1}, param3={2}), exception={3}", param1, param

  • Structured exception handling with a multi-threaded server 2008-10-02

    This article gives a good overview on why structured exception handling is bad. Is there a way to get the robustness of stopping your server from crashing, while getting past the problems mentioned in the article? I have a server software that runs a

  • Best Practice for Exception Handling in a Windows Forms Application? 2008-10-08

    I'm currently in the process of writing my first Windows Forms application. I've read a few C# books now so I've got a relatively good understanding of what language features C# has to deal with exceptions. They're all quite theoretical however so wh

  • Exception handling practices 2008-10-13

    Anyway, I'm a little confused about when to propagate an exception and when to wrap it, and the differences. At the moment, my understanding tells me that wrapping an exception would involve taking an exception like DriveNotFound (in IO) and then wra

  • .NET - What's the best way to implement a "catch all exceptions handler" 2008-10-20

    I'm wondering what the best way is to have a "if all else fails catch it". I mean, you're handling as much exceptions as possible in your application, but still there are bound to be bugs, so I need to have something that catches all unhandled except

  • Exception handling architecture 2008-10-28

    Does anybody have best practices for exception handling ? When searching the web I find a lot of best practices on a code level (don't catch general exceptions, don't rethrow new exceptions etc.) What I am looking for is best practises on a higher le

  • Transparent generic exception handling for asp.net / MOSS2007 (with code) 2008-11-11

    Not exactly a question this, but something that might help out a few people I hope.. Some time ago, I wrote a general transparent exception handling module for use in a MOSS 2007 (Sharepoint) solution. The solution had many web parts, web parts that

  • Async command pattern - exception handling 2008-11-18

    I am implementing an asynchronous command pattern for the "client" class in a client/server application. I have done some socket coding in the past and I like the new Async pattern that they used in the Socket / SocketAsyncEventArgs classes. My async

  • Exception Handling in classes and Code Behind with C# 2008-11-18

    I'm a little bit stuck with a asp.net project that i'm doing! I have got a class that is called from the code behind and many of its function have no return type ie, being void. How does one do exception handling then??? Also, if the function within

  • A strange problem with C++ exception handler 2008-11-26

    I used standard exception handling methods in C++. Which is try{} and catch{} block. In my code, func1() would throw an exception, And func2 is like this: bool func2() { try{ func1(); } catch(myException& e) { cerr << "error!" << endl

  • How to implement one "catch'em all" exception handler with resume? 2008-12-03

    I wonder how can I write a catch'em all exception handler in the application level which will give the user the option to resume the application flow? --------------Solutions------------- I assume you are writing a Windows application in which case,

  • Example sql for logging exceptions to database using exception handling application block 2008-12-10

    Can anyone provide (or point me to) example sql code for logging exceptions using Enterprise Library Exception Handling Application Block (EHAB)? ie I'd like standard CREATE TABLE and CREATE PROCEDURE statements for the structures and procs to store

  • Exception Handling in C#: Multple Try/Catches vs. One 2008-12-16

    Is it good practice to have more than one try{} catch{} statement per method? --------------Solutions------------- In my point of view it is good practice to have each method handle only a single task. As such you'll rarely need to have multiple try/

  • Automated Exception Handling 2008-12-16

    I was wondering if something exists (in Java world) able to take an snapshot of the JVM current state with the following features: Do it while an exception is being thrown. Capture local variables, method's arguments, etc. Put it in a handy file whic

  • If exception handling should be centralised is the design of WinForm components flawed? 2008-12-22

    Or any other design that has the gui widgets as being the first port of call where an unhandled exception will kill the app? We all want one main: "catch all" for face saving purposes (although in most cases this shouldn't have a "continue" feature)

  • Usage of Exception handling block in Enterprise Library 2009-01-02

    We have a ASP.NET application that uses the Exception Handling Application block to log our exception to a database (using logging block indirectly). This all working perfect. However, since it is using the exception handling block to log data, every

  • .Net WFC/Web service exception handling design pattern 2009-01-14

    I'm trying to come up with a simple, easy to use design pattern for error handling in a .net wcf service (specifically a silverlight enabled wcf service). If an exception gets thrown in the service method the silverlight application will see a Commun

  • JSF 1.x generic exception handling 2009-01-20

    If I have an exception in my business layer (e.g. a SQL exception in my JDBC connection bean), how can I propagate it with a custom message to a global error.jsp page? --------------Solutions------------- JSF 1.x doesn't provide any implicit error ha

  • How is the C++ exception handling runtime implemented? 2009-01-29

    I am intrigued by how the C++ exception handling mechanism works. Specifically, where is the exception object stored and how does it propagate through several scopes until it is caught? Is it stored in some global area? Since this could be compiler s

  • Is object oriented exception handling in Perl worth it? 2009-02-02

    I recently read "Object Oriented Exception Handling in Perl" Perl.com article. Is there any point to use exceptions in Perl? --------------Solutions------------- I should note that the article you referenced is old, and that you should now use Except

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.309 (s). 11 q(s)