简体   繁体   中英

Exception Handling in C# with Entity Framwork 4

I use asp.net 4, c# and ef4.

I would like to know what is the best way to catch an generic Exception from Entity Framework.

  • At the moment I use Exception it is appropriate?
  • How catch a more specific one?

Thanks for your time.

try
{
    context.DeleteObject(myLockedContent);
    context.SaveChanges();
}
catch (Exception)
{
    e.Cancel = true;
}

It's rarely good to catch generic exceptions and just cancel them. Exceptions are there to help you ensure you code can act appropriately.

You can catch specific exception types just as you have for the generic (albeit with the identifier you have missed on your example) thus:

catch (OptimisticConcurrencyException ex) 
{
    // Do some real work to resolve the exception
}

The exception type specified in the catch statement tells the runtime to catch that specific and any child exceptions . As a result you need to organise your catch statements from the most specific exception to the least, ie :

catch (OptimisticConcurrencyException ex) 
{
    // Do some real work to resolve the specific exception
}
...
catch (Exception ex) 
{
    // Do some real work to resolve the generic 'catch-all' exception
}

Do not do that.

You are hiding errors which could severely affect the reliability of your application. An exception is thrown for a reason , just continuing on like nothing have happened is just wrong.

You're method cannot return the result as promised, which will affect all code that use it. But the calling methods will not know about the exception and will in worst case continue as nothing have happened and therefore produce undesired results.

You should only use catch all

a) when wanting to wrap exceptions at layer boundaries (but do include the original exception).

b) when the exception have propagated to the top layer (which would terminate your application if the exception is not caught).

Other than that, only catch exceptions when you can handle them . That means that you, by catching the exception, can return the result that the caller expects.

the way you are catching in your example is bad, always log the exception somewhere and somehow, for example on a text file or to an SMTPAppender, you can use Log4Net and get it running in very short time with minimal coding from your side.

Said so, it really depends if you want to handle different exceptions differently, for example if a file was not found you can decide to create it or to tell the user to do something, if the more general exception is thrown you may act differently...

just keep in mind you should put all your catch clauses from the more specific to the more generic one, in your example, if you have multiple catches, the one you wrote should be put in the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM