如何捕捉异常 [英] How to catch exceptions

查看:123
本文介绍了如何捕捉异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是将妥善处理异常我首次应用。这是一个WCF服务。所有其他前很简单的应用程序只为自己。我对在C#中的异常处理非常小知识

This is my first application that will deal with exceptions properly. It's a WCF service. All the other before were simple apps just for myself. I have very small knowledge about exception handling in C#.

我有这样的代码:

MembershipUser memUser = Membership.GetUser(username);

DatabaseDataContext context = new DatabaseDataContext();
UserMembership user = UserMembership.getUserMembership(memUser);
ItemsForUser itemUser = Helper.createItemForUser(ref item, memUser);
Helper.setItemCreationInfo(ref item, user);
context.Items.InsertOnSubmit(item);
context.SubmitChanges();

在这段代码有一些例外可能发生。喜欢的NullReferenceException为例。我怎么知道哪个对象导致异常,所以我可以知道自己在catch做什么和返回到客户端?

In this code a few exceptions can happen. Like NullReferenceException for example. How do I know which object caused the exception so I can know what to do in the catch and what to return to the client?

推荐答案

在一般情况下,你不应该赶上的任何的异常。

In general, you should not catch any exceptions.

我知道这听起​​来很奇怪,但事实是,你应该只捕获异常,你可以真正的的一些事情,和你平时不能做任何的异常。

I know this sounds strange, but the fact is that you should only catch exceptions you can actually do something about, and you usually can't do anything about exceptions.

例外这一规则有什么意思处理异常的事情。在一些应用中,处理的例外装置记录它。在其他国家(ASP.NET,例如),这是最好的的处理异常,因为框架(在这种情况下,ASP.NET运行状况监视)将记录给你。

The "exceptions" to this rule have to do with what it means to "handle" an exception. In some applications, "handling" the exception means to log it. In others (ASP.NET, for instance), it's better to not handle the exception, because the framework (ASP.NET Health Monitoring in this case) will log it for you.

在事件驱动的代码,例如Windows窗体,我觉得有必要赶上事件处理程序中的异常。至少在早期版本的.NET,使异常传播的外界,例如,一个按钮点击事件产生不愉快的结果。我通常捕获异常,并在对话框中显示出来。

In event-driven code, like Windows Forms, I find it's necessary to catch exceptions within event handlers. At least in earlier versions of .NET, allowing the exception to propagate outside of, for instance, a button click event produced unpleasant results. I typically catch the exception and display it in a dialog box.

在一个多层应用程序,人们可以赶上层边界异常,然后再次抛出一个新的,tier-特例:

In a multi-tier application, one might catch exceptions on tier boundaries, then rethrow a new, tier-specific exception:

try
{
   // ...
}
catch (Exception ex)
{
    throw new TierException("Some message", ex);
}



另一个用例是捕获异常,则抛出一个新的异常,并更在它的信息:

Another use case is to catch the exception, then throw a new exception with more information in it:

public int GetValueFromConfigurationFile(...)
{
    const string configFileName = "...";
    try
    {
        // ...
    }
    catch (FileNotFoundException fEx)
    {
        throw new InvalidOperationException(
            String.Format("Can't find configuration file {0}", configFileName), 
            fEx);
    }
}

在这种情况下,正赶上一个特定的异常( FileNotFoundException异常),并提供您的来电信息,他们可能并不知道的:该文件未找到是一个配置文件

In this case, you are catching a specific exception (FileNotFoundException), and providing your caller information they could not otherwise know: that the file not found was a configuration file.

一般的信息是:
- 只抓住你知道如何处理
例外 - 赶上最具体的异常可能
- 创建一个新的异常的时候,要保持始终包括内部异常例外
链 - 重新抛出当前的异常,使用扔; ,而不是罚球前;

The general message is: - Only catch exceptions you know how to handle - Catch the most specific exception possible - Always include the inner exception when creating a new exception, to preserve the chain of exceptions - To rethrow the current exception, use throw;, not throw ex;

有一些更多的,我会试图找到从Microsoft框架设计指南参考。

There are a few more, and I'll try to find you the reference from the Microsoft Framework Design Guidelines.

PS如果有人能找到这个问题,这是一个重复,随意关闭为重复。我不介意从这个丢失任何代表。我只是找不到重复的。

P.S. If someone can find the question this is a duplicate of, feel free to close as a duplicate. I don't mind losing any rep from this. I just couldn't find the duplicate.

我应该有刚刚发布的链接,异常处理< A HREF =htt​​p://stackoverflow.com/tags/exception-handling/info>标签维基,在那里说:

I should have just posted the link to the "exception-handling" tag wiki, where it says:

不过,对于例外.NET程序的情况下处理,见
设计指引
例外
的。

However, for exception handling in the context of .NET programs, see "Design Guidelines for Exceptions".

这篇关于如何捕捉异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆