When is a door not a door...
OK, I just got into a little debate with my cohort Bill Steele over a few lines of code. Why don't you, dear readers, take a look for yourselves:
Private Function DoSomething() As Boolean
Try
Return True
Catch ex As Exception
Finally
MessageBox.Show("Here I am!")
End Try
End Function
Now, without trying out the code, or looking it up in help or a book, answer me this: When the function is called, will the messagebox show up, and why or why not?
Comments
- Anonymous
January 24, 2005
whithout checking I am not sure...
but FWIW I would never code that way....
finally will happen or will normaly happen but return is an exit from the function.....
I would say that return should not be in a try / catch block
if it were after the try/catch/finally
and the try set a local bool flag and the return returned the state of the local bool that would be clear and correct structure.
thats my take on this kind of snip of code. - Anonymous
January 24, 2005
The comment has been removed - Anonymous
January 24, 2005
The finally always happens, even if you Exit Sub or Return from the Try statement.
Personally I write code similar this all the time (although not with msgboxes).
With New <DisposableClass>
Try
...
Return .Result()
Finally
.Dispose
End With
This is basically the VB way of implemeting a Using statement. - Anonymous
January 24, 2005
Glen, I noticed you didn't mention which side you were on. ;-)
Bill - Anonymous
January 24, 2005
Without checking, it absolutely will be called. I would assume that .Net's exception handling is designed this way since the finally block is likely to have resource disposal in it and it's not a good idea to leave those resources hanging. - Anonymous
January 24, 2005
After playing a little, it's interesting to note that you can't branch out of a Finally block. That means no Return or Exit statements. This makse sense because it allows code in either the Try or Catch block to branch without losing the Finally block's code, but also without having to worry about the Finally block hijacking its branch. It's the best of both worlds. - Anonymous
January 24, 2005
Definitly would be called. Has to be. - Anonymous
January 24, 2005
Yes, it will be called. - Anonymous
January 24, 2005
The comment has been removed - Anonymous
January 24, 2005
There is only one code path, and it returns True. :) - Anonymous
January 24, 2005
Because Yoda tells us that there is no try. Only "do" or "do not". - Anonymous
January 24, 2005
The comment has been removed - Anonymous
January 25, 2005
Wow, quite a range of comments here. I should say that I used the word "debate" as if there were 2 sides to the issue. The fact is, as Brian pointed out, the code in Finally will always be called. My initial conclusion, which was confirmed by the online help for VS.NET, was that the Return statement would immediately return control to the calling code. Perhaps this should be amended with "as long as it's not inside a Try/Catch statement" :-) And Stephen, it's too bad you don't use MS products - you could be a lot more productive. Why not come to a local MSDN Event (http://www.msdnevents.com) and we can show you? - Anonymous
January 27, 2005
Yeah it will, save a ThreadAbortException - Anonymous
January 27, 2005
The comment has been removed - Anonymous
June 19, 2009
PingBack from http://mydebtconsolidator.info/story.php?id=7937