Problem with finally clause in C#

Started by
22 comments, last by yaroslavd 20 years ago
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]
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
-----------------SloGameDev.net | My homepageQ:What does a derived class in C# tell to it's parent?A:All your base are belong to us!
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.
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
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).
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.
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]
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 ifreturn obj;




Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
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 ifreturn 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.
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.

This topic is closed to new replies.

Advertisement