Problem with error handling when database connection

on July 5, 2005

I’ve been stuck with a stupid error handling error.

Was using error handling to catch error during a loop to get rows from a Reader.
My error handling would push the error to the upper level. I had a Finally condition that was closing the reader and the connection.

Every time I had an error, the upper level was receiving an unknown type error (or whatever).

The solution:
The problem was a login error. The catch “catched the error” and executed the finally statements. The real problem was that I tried to close a Reader and a connection without testing if it was open. since the error appear before I could open a Reader, I was getting an error during my error trapping. So even if I tried all kind of Exception creation and push it was not receiving the first exception because a new one occurred during the process.

finally
{
if( myReader != null)
myReader.Close();
if( OracleCon != null)
OracleCon.Close();
}

So… When having stuff in a finally clause, make sure that it cannot cause an exception!

ShareThis