C
ChrisB
Hello All:
I notice that when using try/catch blocks in C#, variables declared in the
try block go out of scope in the finally block.
So, for example, the following code generates a compiler error:
try
{
SqlDataReader sqlDataReader = new SqlDataReader(sqlConnection,
CommandType.StoredProcedure, "ConsumerGet", consumerParameters));
// other code
}
finally
{
sqlDataReader.Close() // make sure reader is closed; generates out of
scope error
}
Would it not have been preferable to make objects declared try blocks
accessible in their corresponding finally block. Is there a benefit to this
retriction that I am missing?
Thanks,
Chris
I notice that when using try/catch blocks in C#, variables declared in the
try block go out of scope in the finally block.
So, for example, the following code generates a compiler error:
try
{
SqlDataReader sqlDataReader = new SqlDataReader(sqlConnection,
CommandType.StoredProcedure, "ConsumerGet", consumerParameters));
// other code
}
finally
{
sqlDataReader.Close() // make sure reader is closed; generates out of
scope error
}
Would it not have been preferable to make objects declared try blocks
accessible in their corresponding finally block. Is there a benefit to this
retriction that I am missing?
Thanks,
Chris