Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top