Exception handling - row skipped and sqle is null
IDE = VS7 or 2002
Hi everyone, I have a really weird problem here. The code doesn't seem to execute as expected. I run this through the debugger and it works very strangely.
I made sure the Virtual Directory is using ASP.NET 1.0.3705.
What follows is the code and I will explain what the debugger shows me how the execution steps are in the comments:
try
{
objConnection.Open(); // STARTS HERE
objCommand.ExecuteNonQuery(); // DOES NOT THROW EXCEPTION
int c = 0; // THIS LINE IS EXECUTED
}
catch (SqlException sqle)
{
LogError(); // THIS LINE IS NOT EXECUTED
throw sqle; // THIS LINE IS EXECUTED AFTER THE int c = 0;
// sqle IS NULL
// EXCEPTION IS NOT CAUGHT AND
// EXECUTION CONTINUES IN FINALLY BLOCK
}
finally
{
// EXECUTES AS EXPECTED FROM HERE ON OUT,
// AS THOUGH THE throw sqle; DID NOT HAPPEN.
if (objConnection.State == ConnectionState.Open) objConnection.Close();
}
Has anyone experienced this strange behavior before? Any idea how to fix this? I can change the method a lot, but I would still like to know why this is happening.
I suspect sql is null, so the cast doesn't behave as expected. But why did we jump into this code block first?
I reloaded it several times, saved and rebuilt it, and executed it with the debugger and watched this behavior several times.
Thanks everyone for your help!
All the best
Graham
a source to share
Wait ... your code is not throwing an exception and you are wondering why it is not doing the catch block?
EDIT (referencing your comment):
It sounds very hard to believe. I've never heard of a case where the actual exception inside the catch block was null, and as you mentioned, the first line inside the catch block was not executed, which, in my opinion, indicates that there was no exception.
Have you tried to test the program flow using the old-fashioned debugging methods (Debug.WriteLine) and skipping the debugger?
My guess is that you are looking at where the exception is thrown .. or there is no exception at all.
a source to share
Very strange. I'm not sure what's going on with your code, but one thing I've seen is using:
catch (SqlException sqle)
{
LogError(); // THIS LINE IS NOT EXECUTED
throw sqle; // THIS LINE IS EXECUTED AFTER THE int c = 0;
// sqle IS NULL
// EXCEPTION IS NOT CAUGHT AND
// EXECUTION CONTINUES IN FINALLY BLOCK
}
You want to write:
catch (SqlException sqle)
{
LogError();
throw;
}
To rethrow the exception.
a source to share