471,091 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

Variable scope bug?

I've got a problem with the scope of a variable, consider
the following example:
MyCOMObject.MyObjectClass obj;

try
{
obj = new MyCOMObject.MyObjectClass();
returnXml = obj.getEnquiryUsers();
}
catch (Exception e)
{
//handle error
}
finally
{
if (obj!=null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}

This won't compile, it tells me that the variable obj is
unassigned when I test if it's null in the finally
statement.

This doesn't seem correct to me; I need to release the
COM object in the finally statement as if there is an
error in the try block the object will never be released.
These steps work in Java and are the steps we normally
take when interfacing with COM objects, is this a bug?

Thanks
Andrew

Jul 21 '05 #1
3 989
If the statement "new MyCOMObject.MyObjectClass()" were to throw an
exception then obj would not have been assigned, and thus the warning in the
finally clause.

You need to decide whether exceptions resulting from creating the object
need to be handled here (in which case set obj to null beforehand), or
whether you aren't handling them here (in which case create the object
outside the try block).

HTH,

Stu
"Andrew Todd" <an*********@ukonline.co.uk> wrote in message
news:0b****************************@phx.gbl...
I've got a problem with the scope of a variable, consider
the following example:
MyCOMObject.MyObjectClass obj;

try
{
obj = new MyCOMObject.MyObjectClass();
returnXml = obj.getEnquiryUsers();
}
catch (Exception e)
{
//handle error
}
finally
{
if (obj!=null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}

This won't compile, it tells me that the variable obj is
unassigned when I test if it's null in the finally
statement.

This doesn't seem correct to me; I need to release the
COM object in the finally statement as if there is an
error in the try block the object will never be released.
These steps work in Java and are the steps we normally
take when interfacing with COM objects, is this a bug?

Thanks
Andrew

Jul 21 '05 #2
Andrew Todd <an*********@ukonline.co.uk> wrote:
I've got a problem with the scope of a variable, consider
the following example:
<snip>
This won't compile, it tells me that the variable obj is
unassigned when I test if it's null in the finally
statement.
Indeed, and it's right. Set it to null to start with. Local variables
don't have any default value; they need to be definitely assigned
before they're read.
This doesn't seem correct to me; I need to release the
COM object in the finally statement as if there is an
error in the try block the object will never be released.
These steps work in Java and are the steps we normally
take when interfacing with COM objects, is this a bug?


No, it wouldn't work in Java. Here's an example showing it:

public class Test
{
public static void main(String[] args)
{
String x;
try
{
x = "hello";
}
catch (Exception e)
{
System.out.println (e);
}
finally
{
if (x==null)
{
System.out.println ("Oops");
}
}
}
}

And compiling it:

Test.java:16: variable x might not have been initialized
if (x==null)
^
1 error

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Indeed, and it's right. Set it to null to start with. No, it wouldn't work in Java. Here's an example showing

it:

Of course - you're right, I'd forgotten to set it to null
initially!

Thanks
Andrew
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grant Wagner | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
23 posts views Thread by Russ Chinoy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
2 posts views Thread by Shraddha | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by SRoubtsov | last post: by

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.