C#中的最佳做法的错误处理和传递错误信息

问题描述:

我使用asp.net和我一直在挣扎了简洁的方式来处理错误,如果有任何,到errormessage的传递给用户。例如,我有一个User类,类的UserManager和数据库类。 IMAGINA我想告诉所有的用户,显示我调用该方法GetAllUsers从UserManagerwhich返回用户对象的列表。此方法创建一个数据库对象,并调用方法的executeQuery(查询字符串)。

I am using asp.net and I am always struggling for a neat way to handle errors and if there are any, to pass the errormessage to the user. For example, I have an User class, UserManager class and a Database class. Imagina I want to show all the users, show I call the method GetAllUsers from UserManagerwhich returns a list of User objects. This method creates a Database object and calls the method ExecuteQuery(string query).

编辑:
现在我的问题,想象不顺心的事中的executeQuery()方法(无法打开数据库)。我想通知打开数据库连接时,用户成才了问题。我应该如何做到这一点,处理这一个整洁的方式?

And now my issue, imagine something goes wrong in the ExecuteQuery() method (failed to open database). I want to notify the user someting went wrong when opening the database connection. How should I do this and handle this a neat way?

编辑2:

你会做这样的事情?或以其他方式?

Would you do it something like this? Or otherwise?

public class Database()
{
   private string _Error;

   // property error (only get)

   private void Open()
   {
     try
     {
       // Open DB
       // Fails because of error          
     }
     catch(Exception ex)
     {
       _Error = ex.Message;
     }
   }

   public DataSet ExecuteQuery(string query)
   {
      try
      {
        Open();

        // Execute query

        // return dataset
       }
       catch(Exception ex)
       {
          _Error = ex.Message;
          return null;
       }
   }
}

public class UserManager ()
{
   private string _Error;

   // Get property for Error       

   public List<User> GetAllUsers ()
   {
      Database db = new Database()

      Dataset DS = db.ExecuteQuery("my query goes here");

      (if DS == null)
      {
        _Error = db.Error;
        return null;
      }

   }
}

在上单击事件的用户界面:

In user interface on a click event:

protected void onClick_event(args)
{

  Usermanager userman = new UserManager();
  List<User> users = userman.GetAllUsers();

  if(users == null)
  {
    // make a error panel visible
    pnlError.Visible = true;
    lblError.Text = userman.Error
  }
}

这是一个很好的方法?

我想你错过做异常处理的正确方法。存储即语言/区域设置相关的数据库中的文字是不是真的是一个好主意。该错误也可能不是在它它执行时,方法的调用者知道何意,但code,从数据库中检索值不知道你$的更高目标的背景下有意义C $!C数据库错误的描述也可能不感兴趣的用户所有,为他们的系统就不能得到用户的列表,这就是全部。对于开发人员来说重要的是要知道究竟有错。把这个可见用户也可以告诉他们你不希望他们看到如表名,密码,数据库用户名,这取决于什么异常包含数据(你无法控制)

I think you missed the right way to do exception handling. Storing the text that is language/locale dependent in your database is not really a good idea. The error might also not be meaningful in the context in which it it executed, the caller of the method knows what is intended but the code that retrieves a value from a database does not know about the "higher goals" of your code! The description of the database error might also not interest users at all, for them the system just cannot get the list of users, that's all. For developers it is important to know what exactly got wrong. Putting this visible to users might also show them data you do not want them to see like table names, passwords, database user names, depending on what the exception contains (which you cannot control)

所以,现在该如何处理呢:

So now how to handle it:


  1. 抓住,你处理它的例外,那就是在 onClick_Event

  2. 请到一个文件详细信息,到页面只有哪些用户应该看到

  3. 在您的code(数据库为例如)遵循这样的原则:

  1. Catch the exception where you handle it, that is in the onClick_Event
  2. Log out to a file detailed info, to the page only what users should see
  3. In your code (Database for e.g.) follow this principle:


  • 请不要修改对象的状态,直到很明显,没有什么可以去错了,让您的对象一致的状态。这意味着不启动修改的成员,那么该异常来了,你就必须将它们全部重置为previous状态finally块。

  • 启动您的操作和抛出的问题一个异常(这里不抓住它,毫无意义的,你只存储一个消息或以后检索整个异常!)

  • 成功时,该临时变量复制到对象的成员

这样,你总是有一个一致的状态,干净code的对象。

This way you would always have an object in a consistent state and clean code.