J
Jonathan Wood
Greetings,
I'm having an issue in C#. I'm using ADO.NET but that seems secondary to the
issue.
The following code:
SqlConnection conn;
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{
conn.Close();
}
Won't compile. The compiler complains that conn is used (within the finally
block) without having been initialized (or something to that effect). So I
looked at some examples in some books I have and they move the conn.Open()
prior to the try block.
Well, that compiles okay. But then what happens if the connection cannot be
opened? That error would not be caught.
Am I missing anything?
Thanks.
I'm having an issue in C#. I'm using ADO.NET but that seems secondary to the
issue.
The following code:
SqlConnection conn;
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{
conn.Close();
}
Won't compile. The compiler complains that conn is used (within the finally
block) without having been initialized (or something to that effect). So I
looked at some examples in some books I have and they move the conn.Open()
prior to the try block.
Well, that compiles okay. But then what happens if the connection cannot be
opened? That error would not be caught.
Am I missing anything?
Thanks.