ODBCException not being caught

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.
 
M

Miha Markic [MVP C#]

Hi Jeff,

Shouldn't be System.Data.Odbc.OdbcException?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Jeff Evans said:
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.
 
G

Guest

Shouldn't be System.Data.Odbc.OdbcException?

Unfortunately, no. As far as I know, the System.Data.Odbc namespace is only present in the 1.1 framework, and the current company policy is to use 1.0. (See:

http://msdn.microsoft.com/netframework/technologyinfo/overview/whatsnew/default.aspx

). I believe the functionality is very similar, though. In any case, I'm completely baffled, since

ex.GetType().ToString()

definitely gives ("Microsoft.Data.Odbc.OdbcException"), but the framework does not recognize it as that type at runtime (cannot catch or cast it). Grr...
 
M

Miha Markic [MVP C#]

Hi Jeff,

Jeff Evans said:
Unfortunately, no. As far as I know, the System.Data.Odbc namespace is
only present in the 1.1 framework, and the current company policy is to use
1.0. (See:http://msdn.microsoft.com/netframework/technologyinfo/overview/whatsnew/default.aspx

Ah, I wondered if you are using 1.0.
). I believe the functionality is very similar, though. In any case, I'm completely baffled, since

ex.GetType().ToString()

definitely gives ("Microsoft.Data.Odbc.OdbcException"), but the framework
does not recognize it as that type at runtime (cannot catch or cast it).
Grr...

It is weird though. I can't reproduce as I don't have .net 1.0 handy.
Is it possible that your exception comes from different assembly? What's its
fullname?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top