Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception handling not working 1

Status
Not open for further replies.

GabeC

Programmer
Apr 17, 2001
245
US
What is wrong with this code? From what I have read it should work just fine. According to this post it should work but it doesn't. thread732-448638

My code can't see the
Code:
oConn
object in the catch portion.

I am getting this error when I build the object.
The type or namespace name 'oConn' could not be found (are you missing a using directive or an assembly reference?)

Code:
try
 {
  SqlConnection oConn = new SqlConnection(strConnStr);
  oConn.Open();

  SqlCommand oCmd = new SqlCommand(sqlCmd, oConn);
  oCmd.CommandText = sqlCmd;
  oCmd.CommandType = CommandType.Text;
  
  ...

}
catch (Exception e)
{
 oConn.Close();
}
 
That's because you declare [tt]oConn[/tt] in the [tt]try[/tt] portion. It's not scoped int he [tt]catch[/tt]. You should instead:

[tt]SqlConnection oConn;

try
{
oConn = new SqlConnection(strConnStr);

...
}
catch (Exception e)
{
oConn.Close();
}[/tt]

Though, I'd probably use [tt]finally[/tt] instead of [tt]catch[/tt] for that.
 
That was fast. Thanks rosenk.

I will put the oConn.Close() in the finally. I just wanted to show I couldn't reference the oConn object.

When I read documentation and threads such as the one listed above that the scope of an object spans the try catch finally block it seems to me that anything declared in the try section would have scope in the catch and finally blocks also.

Since declaring an object in the try block is not visible to the catch and finally blocks, what exactly does it mean that scope spans the try catch finally block?

Gabe

 
OK, I've updated my code and now I am getting the error Use of unassigned local variable 'oConn'. I have put in bold in the following code the line it is referencing.

This article says it is related to not creating an instance of the object in question.

I declare the variable and create an instance of the objecct. Why would I be getting this error?

Code:
string iSucceeded = "";
SqlConnection oConn;
SqlCommand oCmd;

try
{
 oConn = new SqlConnection(strConnStr);
 oConn.Open();

 oCmd = new SqlCommand(sqlCmd, oConn);
 oCmd.CommandText = sqlCmd;
 oCmd.CommandType = CommandType.Text;

 if (oCmd.ExecuteNonQuery() > 0)
 {
  //worked
  iSucceeded = bTrue;
 }
 else 
 {
  //didn't work
  iSucceeded = bFalse;
 }
}
catch (Exception e)
{
 strLastError = e.ToString();
 iSucceeded = bFalse;
}
finally
{
oConn.Close();
Code:
 oCmd = null;
 oConn = null;
}
return iSucceeded;

Gabe
 
That will happen if the statement
[tt]oConn = new SqlConnection(strConnStr);[/tt]
fails, since nothing will have been assigned to oConn.

The right thing to do is probably something like:
[tt]SqlConnection oConn;

try
{
oConn = new SqlConnection(strConnStr);
try
{
oConn.Open();
}
catch (Exception e)
{
}
finally
{
oConn.Close();
}
}
finally
{
oConn = null;
}[/tt]

(I cut out everything not directly related to [tt]oConn[/tt] for brevity.)

You just want to always make sure that you don't use a variable in a [tt]catch[/tt] or [tt]finally[/tt] that might not be in the state you expect.
 
You can also get rid of the "unassigned variable" message by doing this:
Code:
SqlConnection oConn = null;
try
{
  //...and so forth
By assigning null to it you make the compiler happy.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Except that then you would get a runtime error when, in the [tt]finally[/tt] block, you tried to [tt]oConn.Close();[/tt]
 
You do have to check it for null before using it (calling the .Close method or looking at the .State property) in the finally block, yes.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top