Problems with try...catch

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.
 
R

RH

This will solve your problem:

SqlConnection conn = null;
try
{
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}

but you really should be using the "using" keyword for these kinds of
things:

using (SqlConnection conn = new SqlConnection(...))
{
//...
}
 
M

Marc Gravell

I think you snipped away the problem...
I think you originally had:

SqlConnection conn;
try {
conn = new SqlConnection(cs);
conn.Open();
...
} finally {
conn.Close();
}

The problem is that if the first line errors then conn will not have
been initialised. You could get around this by moving the initialiser
into the variable declaration line, but a better option is "using"
since this also manages disposal (which inclused closure), hence:

using(SqlConnection conn = new SqlConnection(cs)) {
conn.Open();
...
conn.Close();
}

For a full explanation, look up "using" and "Dispose".

Marc
 
S

sloan

SqlConnection conn = null;
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{ if(null!=conn)
{
}
}


You can try that, or the "using" statement.
 
M

Marc Gravell

Actually, that could break the "finally" if the conn is never
successfully created (i.e. conn is null when conn.Close() is invoked).
I would recommend the "using" approach as it deals with this for you.
If you want you can still use the following inside the "using".

conn.Open();
try {
...
} finally {
conn.Close()
}

Marc
 
F

ForrestPhoto

Actually, that could break the "finally" if the conn is never
successfully created (i.e. conn is null when conn.Close() is invoked).
I would recommend the "using" approach as it deals with this for you.
If you want you can still use the following inside the "using".

This is a much better approach, and using blocks have their own
try/catch/finally code built-in behind the scenes.

using(SqlConnection conn = new SqlConnection("...")) {
conn.Open();
...
}

Because the SqlConnection class implements IDisposable, the compiler
can write code to catch an error ( say the server can't be reached and
your open call fails ) and destroy the connection, if it exists.
 
J

Jonathan Wood

Yes Marc, I did lose one line that was in question.

I'm not really sure what the using keyword has to do with either error
handling or Dispose, but will research them further.
 
J

Jonathan Wood

Thanks but that article doesn't address the issue I'm raising. I understand
reference type variables must be initialized before being used. The issue
was how to do this and still have code that could fail wrapped in
try...catch.
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
Yes Marc, I did lose one line that was in question.

I'm not really sure what the using keyword has to do with either error
handling or Dispose, but will research them further.

"using" has two uses:

1) The using directive which makes namespaces (and members etc)
available without specifying the namespace when a type is used.

2) The using statement which is an automatically try/finally which
calls Dispose on implementations of IDisposable automatically.
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
What if I want a catch block? What would that look like?

In that case you'd need to put a try/catch either inside or outside the
using statement - there isn't a "using with catch".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top