Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

yaroslavd

Problem with finally clause in C#

This topic is 5347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, guys. In one of my methods, I compare two things. If they are NOT equal, I want to throw an exception. Then, regardless of if they are equal, I want to return a value. Naturally, I tried using a finally clause because that's what it's for.
try {
  if(n1!=n2)
    throw AnException(args);
}
finally {
  return obj;
}
I know you can do that in Java, but my C# compiler doesn't like this. It gives me this error: C:\Documents and Settings\Yaroslav\My Documents\Visual Studio Projects\TestApp\Recorder.cs(117): Control cannot leave the body of a finally clause Any ideas? Thanks in advance. [edited by - yaroslavd on March 28, 2004 3:20:18 PM]

Share this post


Link to post
Share on other sites
Advertisement
try
{
if(n1!=n2)
throw AnException(args);
}
finnaly
{
return somtin;
}


I think this shoudl wokr, but I am not shure

.:3delavnica.com:. Slovenian digital colture
Another day, another bug

Share this post


Link to post
Share on other sites
Yeah, sorry, I forgot to add the try clause first. I guess I edited it in right before you posted. However, it still doesn''t work.

Share this post


Link to post
Share on other sites
quote:
Original post by yaroslavd If they are NOT equal, I want to throw an exception. Then, regardless of if they are equal, I want to return a value.

The exception is going to exit the function, not the return. You can exit a function via a return or an exception, but not both. That''s why you can''t return from a finally.

What exactly are you trying to do? It smells a little fishy

Share this post


Link to post
Share on other sites
Basically, the method reads in an object. You pass an object into it. All the fields that were serialized in the serialized object are read into the passed object. However, if the versions of the said objects differ, some of the passed object''s fields will remain unassigned. I wanna throw an exception telling the user which fields were left unassigned. However, I still wanna return the deserialized object (with default values for unassigned fields).

Share this post


Link to post
Share on other sites
quote:
Original post by yaroslavd
I wanna throw an exception telling the user which fields were left unassigned. However, I still wanna return the deserialized object (with default values for unassigned fields).
And that makes sense to you, that you''d throw and exception and return a value? What use is the value when an exception has been thrown (unless you''re misusing exceptions)?

The finally clause is for cleanup that should be executed regardless of exit path.

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
quote:
Original post by yaroslavd
I wanna throw an exception telling the user which fields were left unassigned. However, I still wanna return the deserialized object (with default values for unassigned fields).
And that makes sense to you, that you'd throw and exception and return a value? What use is the value when an exception has been thrown (unless you're misusing exceptions)?

The finally clause is for cleanup that should be executed regardless of exit path.


Why wouldn't it make sense? Java does it. For example,


try{
object o = AMethod(2);
}
catch(SomeException e)
{
}


In this scenario, o would still get assigned the value returned from AMethod(), yet an exception will be thrown to signal that some error has occured. However, this isn't supported in C#, I guess.

Any way to get around this? I thought of putting the object in my exception. That way, the user can get the partially-loaded object through the exception instance. Good idea?

[edited by - yaroslavd on March 28, 2004 3:54:19 PM]

[edited by - yaroslavd on March 28, 2004 3:56:44 PM]

[edited by - yaroslavd on March 28, 2004 3:58:51 PM]

Share this post


Link to post
Share on other sites
You''re grossly mis-using exceptions, when a simple if-then-else
will do the job.

From your description, it is not an exception when two
objects are not equal. Rather, it''s just a control branching
condition.


object obj;

if (n1 != n2)
{
obj = new MyObject(); // Assign a properly constructed object

}
else
{
obj = MyObject.DefaultObject; // Assign a default object

}
// end if


return obj;





Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
You''re grossly mis-using exceptions, when a simple if-then-else
will do the job.

From your description, it is not an exception when two
objects are not equal. Rather, it''s just a control branching
condition.


object obj;

if (n1 != n2)
{
obj = new MyObject(); // Assign a properly constructed object

}
else
{
obj = MyObject.DefaultObject; // Assign a default object

}
// end if


return obj;





Kami no Itte ga ore ni zettai naru!


I don''t think so. It would be exceptional for the versions to be different. I know I didn''t give you enough information in my post, so you wouldn''t know this. However, it''s too complex to briefly describe.

Like I said, I think I''ll pass the object into the exception class, and that''s how the user will be able to access it.

Share this post


Link to post
Share on other sites
quote:
Original post by yaroslavd
Why wouldn''t it make sense? Java does it. For example,


try{
object o = AMethod(2);
}
catch(SomeException e)
{
}


In this scenario, o would still get assigned the value returned from AMethod(), yet an exception will be thrown to signal that some error has occured.

No, it wouldn''t. An exception stops the normal flow of execution. If AMethod threw, o would not be assigned to, and control flow would pass immediately to the catch.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!