473,698 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

troubles with finally statement and objects

Hi,

I am using the try catch finally block in many places in my code.

When a create an sqldatareader dr and try to close it in my
finally block I get the error: use of unsigned local variable.

dr.close();
works fine in the try but not in the finally.

How can I access my local objects in the finally without these errors?

thanks

Chris
Nov 17 '05
16 2457
Chris,

You can set a try block nested which is very nice for this kind of
operations.

try{open whatever;
try{}catch{}
}
catch{}
finally{close whatever;}

I hope this helps,

Cor
Nov 17 '05 #11
Thanks Everyone for your responses.

I am having trouble finding the namespace that provides me with the
command: CommandBehavior .CloseConnectio n.

I'm getting the type or namespace missing error.
Could someone direct me how get it up and running. I have a hunch it's not
really
my namespace missing but a scope issue?

thanks

chris

"Chris" wrote:
Hi,

I am using the try catch finally block in many places in my code.

When a create an sqldatareader dr and try to close it in my
finally block I get the error: use of unsigned local variable.

dr.close();
works fine in the try but not in the finally.

How can I access my local objects in the finally without these errors?

thanks

Chris

Nov 17 '05 #12
Chris <Ch***@discussi ons.microsoft.c om> wrote:
Thanks Everyone for your responses.

I am having trouble finding the namespace that provides me with the
command: CommandBehavior .CloseConnectio n.

I'm getting the type or namespace missing error.
Could someone direct me how get it up and running. I have a hunch it's not
really my namespace missing but a scope issue?


CommandBehavior is an enumeration in the System.Data namespace.

If that doesn't help, perhaps you could give an example of how you're
trying to use it?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
Jon wrote:
using (SqlDataReader data = command.Execute Reader
(CommandBehavio r.CloseConnecti on))


What would happen if the ExecuteReader command returned null? Does the
using construct check for null before it calls .Dispose?

Nov 17 '05 #14

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Jon wrote:
using (SqlDataReader data = command.Execute Reader
(CommandBehavio r.CloseConnecti on))
What would happen if the ExecuteReader command returned null?


I don't see "null" listed as a possible return value.
Does the
using construct check for null before it calls .Dispose?


In general, I would expect to get an exception if the "using" statement was
executed on "null".
Nov 17 '05 #15
Chris Dunaway <du******@gmail .com> wrote:
Jon wrote:
using (SqlDataReader data = command.Execute Reader
(CommandBehavio r.CloseConnecti on))


What would happen if the ExecuteReader command returned null? Does the
using construct check for null before it calls .Dispose?


Yes. From the C# spec (reformatted with sane bracing):

<quote>
A using statement of the form

using (R r1 = new R())
{
r1.F();
}

is precisely equivalent to

R r1 = new R();
try
{
r1.F();
}
finally
{
if (r1 != null) ((IDisposable)r 1).Dispose();
}
</quote>

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #16
Scott Roberts <sc***********@ no-spam.intelebill .com> wrote:
Does the
using construct check for null before it calls .Dispose?


In general, I would expect to get an exception if the "using" statement was
executed on "null".


No - you won't get an exception unless the body of the using statement
tries to dereference the null value.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3036
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers. It works on an IBM mainframe under AIX with GCC, but has trouble on Intel with GCC (DJGPP). I think it's an endian problem. The trouble is, although most values in the extracted list are correct,
16
7941
by: Ken | last post by:
What is the purpose of a finally block in try-catch- finally? I am confused because, if I am reading the definition correctly, this block seems unnecessary. Consider the following from the Visual Studio documentation. In the first foo(), the statement in the finally block will execute even though there is an exception in the try block. In the second foo(), I took the statement out of the finally block but it should still execute since it...
10
1456
by: RepStat | last post by:
If I have code such a SqConnection cndb = new SqlConnection(connstr) SqlDataReader dr = null tr cndb.Open() SqlCommand cmd = new SqlCommand("exec mysp", cndb) dr = cmd.ExecuteReader()
23
3064
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
26
2552
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
15
2121
by: tshad | last post by:
Do I still need to close an object in a finally statement after a catch if it is part of a using statement? For example: FileStream fs = null; try { using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" + (string)dr))
5
7137
by: Hillbilly | last post by:
MSDN Remarks "as a rule" the using statement should be used when instantiating objects which inherit IDisposable. Other than the obvious unmanaged objects like the file system example, fonts and the database how else may it be determined which classes and their objects inherit IDisposable? And the remarks imply the using statement is a different and perhaps more efficient way to use objects than try-catch-finally? ...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.