简体   繁体   English

使用实体框架4在C#中进行异常处理

[英]Exception Handling in C# with Entity Framwork 4

I use asp.net 4, c# and ef4. 我使用asp.net 4,c#和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? 目前我使用Exception它是否合适?
  • 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 . catch语句中指定的异常类型告诉运行时捕获特定的和任何子异常 As a result you need to organise your catch statements from the most specific exception to the least, ie : 因此,您需要将catch语句从最特定的异常组织到最少,即:

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 你应该只使用catch all

a) when wanting to wrap exceptions at layer boundaries (but do include the original exception). a)何时想要在层边界处包装异常(但包括原始异常)。

b) when the exception have propagated to the top layer (which would terminate your application if the exception is not caught). b)当异常传播到顶层时(如果未捕获异常,将终止您的应用程序)。

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. 您在示例中捕获的方式很糟糕,总是在某处以某种方式记录异常,例如在文本文件或SMTPAppender上,您可以使用Log4Net并在很短的时间内以最少的编码来运行它。

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. 请记住,您应该将所有catch子句从更具体的catch子句放到更通用的子句中,在您的示例中,如果您有多个catch,那么您编写的那个应该放在最后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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