views:

84

answers:

2

IDE = VS7 or 2002

Hi all, I have a really weird problem here. The code doesn't appear to be executing as expected. I'm running this through the debugger and it's performing really strangely.

I have made sure that the Virtual Directory is using ASP.NET 1.0.3705.

The code follows and I explain what the debugger shows me as the execution steps 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 behaviour before? Any idea how to fix it? I may change the method extensively, but I'd still like to know why this is happening.

I suspect since the sqle is null that is why the throw does not behave as expected. But why did we jump into this code block in the first place?

I have reloaded it several times, saved and rebuilt, and executed with the debugger and watched this behaviour multiple times.

Thank you all for your help!

All the best,

Graham

A: 

Wait.. your code doesn't throw an exception and you're wondering why it doesn't execute the catch block?

EDIT (referencing your comment):

That sounds really hard to believe. I never heard of a case where the actual exception inside the catch block was null and as you already mentioned did the first line inside the catch block not execute which in my opinion points into the direction that there was no exception at all.

Did you try to check the program flow by using old-fashioned debugging techniques (Debug.WriteLine) and skipping the debugger?

My assumption is that you're looking at the wrong place where the exception is thrown.. or there's no exception at all.

VVS
There is a catch that executes even though there is no exception thrown. Within that catch there is a throw [I didn't write this code] and the throw line executes but the exception is null and it is not caught.
Graham H
itsmatt and David, The problem as far as I can tell is with the debugger. I followed David's advice to use Debug.WriteLine(). I had another piece of code that was experiencing similar problems. An exception should have occurred but did not. The debugger looked at the if statement and decided it should run the code within - which should cause an exception. Looking at the WriteLine statements I can see that the code block within the if statement did not actually run. It's a difference between how the debugger thinks the code will execute and how it actually executes. Thanks for your help
Graham H
IIRC, that version of the debugger had issues. It would appear as if you would be executing one line of code, then it would skip lines, or do other strange things. I think it was fixed in later versions. You may be able to straighten it out by clearing out the bin folder and rebuilding.
Chris Dunaway
See my comment on the question. I should have posted it here instead..
VVS
+1  A: 

Very strange. I'm not sure what's going on with your code, but one thing I saw is the use of:

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 re-throw the exception.

itsmatt
The first version isn't wrong at all. You're just killing the stack trace.
VVS
I didn't say that it was wrong, but it would certainly be more useful to see the full stack trace, typically. Thanks for pointing that out. I had neglected to put that in my comment.
itsmatt