title: C# best practice error handling and passing error messages
body: <p>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). </p>
  
  <p>EDIT:
  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?</p>
  
  <p>EDIT 2:</p>
  
  <p>Would you do it something like this? Or otherwise? </p>
  
  <pre><code>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&lt;User&gt; GetAllUsers ()
     {
        Database db = new Database()
  
        Dataset DS = db.ExecuteQuery("my query goes here");
  
        (if DS == null)
        {
          _Error = db.Error;
          return null;
        }
  
     }
  }
  </code></pre>
  
  <p>In user interface on a click event:</p>
  
  <pre><code>protected void onClick_event(args)
  {
  
    Usermanager userman = new UserManager();
    List&lt;User&gt; users = userman.GetAllUsers();
  
    if(users == null)
    {
      // make a error panel visible
      pnlError.Visible = true;
      lblError.Text = userman.Error
    }
  }
  </code></pre>
  
  <p>Is this a good approach?</p>