G
Guest
Hello. Let me start with a code snippet:
int affected;
try
{
affected = myCommand.ExecuteNonQuery();
}
catch (Microsoft.Data.Odbc.OdbcException ex)
{
//Debug.WriteLine(something);
}
catch (System.Data.OleDb.OleDbException ex)
{
//Debug.WriteLine(something else);
}
catch (Exception ex)
{
Debug.WriteLine(ex.GetType().ToString());
}
the myCommand object is an IDbCommand, and its connection is an IDbConnection (both created by a proprietary abstract factory). Whenever the query results in something like a database constraint violation (foreign key, duplicate key, etc.) or just any general SQL syntax error, an Exception is of course thrown. However, it is not caught until my final catch block (Exception ex). At this point, the type is clearly Microsoft.Data.ODBC.ODBCException, as indicated by debugging. So why isn't it being caught in the correct catch block? I also try casting Exception ex to ODBCException _ex, which itself throws an InvalidCastException. So what is going on here? Why doesn't the runtime handle this exception properly, when I am explicitly trying to catch its very type? Any suggestions? Thanks.
int affected;
try
{
affected = myCommand.ExecuteNonQuery();
}
catch (Microsoft.Data.Odbc.OdbcException ex)
{
//Debug.WriteLine(something);
}
catch (System.Data.OleDb.OleDbException ex)
{
//Debug.WriteLine(something else);
}
catch (Exception ex)
{
Debug.WriteLine(ex.GetType().ToString());
}
the myCommand object is an IDbCommand, and its connection is an IDbConnection (both created by a proprietary abstract factory). Whenever the query results in something like a database constraint violation (foreign key, duplicate key, etc.) or just any general SQL syntax error, an Exception is of course thrown. However, it is not caught until my final catch block (Exception ex). At this point, the type is clearly Microsoft.Data.ODBC.ODBCException, as indicated by debugging. So why isn't it being caught in the correct catch block? I also try casting Exception ex to ODBCException _ex, which itself throws an InvalidCastException. So what is going on here? Why doesn't the runtime handle this exception properly, when I am explicitly trying to catch its very type? Any suggestions? Thanks.