使用 Entity Framwork 4 进行 C# 异常处理
我使用 asp.net 4、c# 和 ef4。
我想知道从实体框架捕获通用异常的最佳方法是什么。
- 目前我使用
Exception
它是合适的吗? - 如何捕获更具体的一个?
感谢您抽出时间。
try
{
context.DeleteObject(myLockedContent);
context.SaveChanges();
}
catch (Exception)
{
e.Cancel = true;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捕获通用异常并取消它们很少是好的。异常可以帮助您确保代码能够正确运行。
您可以捕获特定的异常类型,就像捕获泛型异常一样(尽管您在示例中遗漏了标识符),因此:
catch 语句中指定的异常类型告诉运行时捕获该特定的和任何子异常< /强>。因此,您需要从最具体的异常到最少的异常来组织 catch 语句,即:
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:
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, i.e. :
不要那样做。
您隐藏的错误可能会严重影响应用程序的可靠性。 抛出异常是有原因的,像什么都没发生一样继续下去是错误的。
您的方法无法按承诺返回结果,这将影响使用它的所有代码。但是调用方法不会知道异常,并且在最坏的情况下会继续下去,因为什么也没有发生,因此会产生不希望的结果。
您应该只使用 catch all
当想要在层边界包装异常时(但要包括原始异常),
a)。 b) 当异常传播到顶层时(如果未捕获异常,则应用程序将终止)。
除此之外,仅在可以处理的情况下捕获异常。这意味着您通过捕获异常可以返回调用者期望的结果。
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.
您在示例中捕获的方式很糟糕,总是在某处以某种方式记录异常,例如在文本文件或 SMTPAppender 上,您可以使用 Log4Net 并使其在很短的时间内运行,只需最少的编码。
这么说,这实际上取决于您是否想要以不同的方式处理不同的异常,例如,如果找不到文件,您可以决定创建它或告诉用户做某事,如果抛出更一般的异常,您可能会采取不同的行为。 ..
请记住,您应该将所有 catch 子句从更具体的子句放到更通用的子句中,在您的示例中,如果您有多个 catch,则您编写的子句应该放在最后。
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.