I have some code here:
public static void OpenConnection(IDbConnection connection)
{
if(connection == null)
throw new ArgumentNullException("connection", "The connection was null.");
if (connection.State != ConnectionState.Closed)
connection.Close();
}
The code has to be executed quite a lot since I open and close the connection every time I do something in the database. I wonder if the next code would be a better solution performance wise:
public static void OpenConnection(IDbConnection connection)
{
try
{
connection.Close();
}
catch (NullReferenceException nullReferenceException) { throw; }
catch (Exception exception) { } // This will occur if the connection was already closed so nothing should be done then.
}
PS. Is the catch (Exception exception) { }
necessary?
EDIT: Replaced ArgumentNullException
by NullReferenceException
in the second code since that will be the exception when the connection == null
.