Good question! I must confess that I thought about this quite long and I learned that there is no hard and fast rule to this. So here's my take on it :
Ask yourself: Does this code need to communicate error states to the calling code? If so, what kind of information does it need to pass on? Will a simple boolean (indicating success or failure of the operation) be sufficient or is detailed information required (for logging, for example)? Depending on the answers to the above questions, you have a few options - Either bubble up any caught exceptions as-is to the calling code (optionally wrapping it up with additional information) OR throw a custom exception that is designed specifically for this kind of exception state, OR suppress all exceptions and just return a boolean indicating success or failure. On May 9, 6:17 pm, jamesmcgeachie <[email protected]> wrote: > Hi > > I am quite new to C#/Asp.Net and having some difficulties using error > handling in a Class. > > Currently there is no error handling in the Class > - The Class is compiled to a DLL and used to execute an SQL stored > procedure > - This class is then called from an ASPX page > > The question is, how do I add exeption handling to the Class which > will return an error or success message to the ASPX page where it was > called. > > Any help is greatley appreciated > > Thanks > James > > Class Code: > public class sqltest // > { > > public sqltest(string a, string b) > { > > SqlConnection sqlConn = new SqlConnection > (ConfigurationSettings.AppSettings["TestDatabaseConnection"]); > SqlCommand sqlCmd = new SqlCommand("TestStoredProcedure", > sqlConn); > sqlCmd.CommandType = CommandType.StoredProcedure; > > SqlParameter sqlParam = null; > > sqlParam = sqlCmd.Parameters.Add("@FirstValue", > SqlDbType.VarChar, 50); > sqlParam.Value = a; > > sqlParam = sqlCmd.Parameters.Add("@SecondValue", > SqlDbType.VarChar, 50); > sqlParam.Value = b; > > sqlConn.Open(); > > sqlCmd.ExecuteNonQuery(); > > sqlConn.Close(); > } > } > > ASPX.CS Code > protected void Button1_Click(object sender, EventArgs e) > { > sqltest callCMD = new sqltest(TextBox1.Text, TextBox2.Text); > }
