Assertion vs. Exception
-
This is a simple class I am building to abstract some functionality. Pay attention to my use of assertions. /// /// Encapsulates execution of queries. /// public class QueryExecutor { /// summary /// Returns a DataTable object populated using the specified parameters. /// summary /// param name="connectionString" // the connection string to use /// param name="queryType" // /// param name="queryText" /// returns public static DataTable GetTable(string connectionString, CommandType queryType, string queryText) { Trace.Assert(queryText != null, "QueryExecutor::GetData\nQuery must not be null."); Trace.Assert(queryText != string.Empty, "QueryExecutor::GetData\nQuery must not be empty."); Trace.Assert(connectionString != null, "QueryExecutor::GetData\nConnection must not be null."); Trace.Assert(connectionString != string.Empty, "QueryExecutor::GetData\nConnection must not be empty."); SqlConnection connection = new SqlConnection(connectionString); SqlCommand query = connection.CreateCommand(); query.CommandType = queryType; query.CommandText = queryText; SqlDataAdapter adapter = new SqlDataAdapter(query.CommandText, connection); DataTable table = new DataTable(); try { adapter.Fill(table); } catch (SqlException) { ReleaseResources(adapter, connection); throw; } catch (Exception) { ReleaseResources(adapter, connection); throw; } return table; } private static void ReleaseResources(SqlDataAdapter adapter, SqlConnection connection) { adapter.Dispose(); if (connection.State == ConnectionState.Open) connection.Close(); } } I am trying to fully understand the difference between assertions and exceptions. According to readings I have read, I have come to conclusion that assertions are used for errors which should never occur and thus programmers can fix it, and for class invariances. In other words it warns the programmer and helps the programmer know that the code is actually doing what it was intended to do. Exception on the other hand can not be
-
This is a simple class I am building to abstract some functionality. Pay attention to my use of assertions. /// /// Encapsulates execution of queries. /// public class QueryExecutor { /// summary /// Returns a DataTable object populated using the specified parameters. /// summary /// param name="connectionString" // the connection string to use /// param name="queryType" // /// param name="queryText" /// returns public static DataTable GetTable(string connectionString, CommandType queryType, string queryText) { Trace.Assert(queryText != null, "QueryExecutor::GetData\nQuery must not be null."); Trace.Assert(queryText != string.Empty, "QueryExecutor::GetData\nQuery must not be empty."); Trace.Assert(connectionString != null, "QueryExecutor::GetData\nConnection must not be null."); Trace.Assert(connectionString != string.Empty, "QueryExecutor::GetData\nConnection must not be empty."); SqlConnection connection = new SqlConnection(connectionString); SqlCommand query = connection.CreateCommand(); query.CommandType = queryType; query.CommandText = queryText; SqlDataAdapter adapter = new SqlDataAdapter(query.CommandText, connection); DataTable table = new DataTable(); try { adapter.Fill(table); } catch (SqlException) { ReleaseResources(adapter, connection); throw; } catch (Exception) { ReleaseResources(adapter, connection); throw; } return table; } private static void ReleaseResources(SqlDataAdapter adapter, SqlConnection connection) { adapter.Dispose(); if (connection.State == ConnectionState.Open) connection.Close(); } } I am trying to fully understand the difference between assertions and exceptions. According to readings I have read, I have come to conclusion that assertions are used for errors which should never occur and thus programmers can fix it, and for class invariances. In other words it warns the programmer and helps the programmer know that the code is actually doing what it was intended to do. Exception on the other hand can not be
Have a google for "Design by Contract" and "nContract".
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Have a google for "Design by Contract" and "nContract".
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.Thanks for the answer but I am designing based on design by contract. I was hoping someone can answer my questions which were unanswered even after numerous reads. I am still waiting for an answer as that does not help me whatsoever. I have read so many readings based on this topic and seems like not too many people understand it, hence the reason for only 1 reply to my question.
-
Thanks for the answer but I am designing based on design by contract. I was hoping someone can answer my questions which were unanswered even after numerous reads. I am still waiting for an answer as that does not help me whatsoever. I have read so many readings based on this topic and seems like not too many people understand it, hence the reason for only 1 reply to my question.
Ah ok, well I'll put in my 2c on your questions then. 1. Assertions and Exceptions are pretty different concepts. Assertions are a test that you absolutely expect to succeed, and indicates a design/breach of contract error if it fails. A failing assertion needs to perform an action, and this would commonly be throwing an exception (or getting a TraceListener to do something). I'd tend towards throwing an ArgumentException in these cases. 2. Trace is like Debug. Instead of the DEBUG conditional compilation constant, it is dependant on TRACE being defined (which is defined in release mode by default). 3. It won't compile. ReleaseResources can't see adapter or connection due to scoping. Even if you made these static, your method wouldn't be thread-safe. 4. IMO theres no need to "enterprise" up a DAL with all sorts of abstraction if you are doing simple enough things that could be handled by the built-in IDE databinding. If you do need a proper ORM solution, then coding one by hand with sufficient functionality is going to take a long time.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.