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
Return True
Catch ex As Exception
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 (16)
  1. Denny says:

    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.

  2. Ron Krauter says:

    Without trying…the message box will be called because as far as I can remember, the finally block is *guaranteed* to be called after a try block is executed.

  3. Will Waters says:

    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>


    Return .Result()



    End With

    This is basically the VB way of implemeting a Using statement.

  4. Glen, I noticed you didn’t mention which side you were on. 😉


  5. Brian says:

    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.

  6. Brian says:

    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.

  7. SuperJason says:

    Definitly would be called. Has to be.

  8. Paul Murphy says:

    Yes, it will be called.

  9. Jeff Parker says:

    Aside from the fact it shouldn’t compile because all code paths do not return anything, nothing in the catch block but asside from that.

    Will is correct, I did have to look at how a VB Function was declared. I am a C# man myself so looked a little odd having the As Boolean at the end. But the Message Box will show before the value is returned.

    Hmm no that is Not correct sort of, can’t think of the words I want to use. But Essentially the value is returned, but the execution of the method is not complete hence the caller of the Method has not proceded to do anything.

    Perhaps a better example of this in C# console app sorry can’t throw out VB as easy as C,

    public class MyClass


    private static bool myvalue = true;

    public static void Main()






    private static bool returnTrue()




    return myvalue;








    for(int LoopCount = 0; LoopCount < 5000; LoopCount++)


    Console.WriteLine("Finally Block.");


    myvalue = false;




    Anyway Learned this the hard way a long time ago on the .net Terrarium http://www.windowsforms.net/Applications/application.aspx?PageID=30&tabindex=8

    To build an insect on there, some rule, first must run really fast have only a couple hundred milliseconds for complete execution of your insect, and two must absolutely throw no errors.

  10. amotif says:

    There is only one code path, and it returns True. 🙂

  11. Because Yoda tells us that there is no try. Only "do" or "do not".

  12. This is a bit much,

    when ms programmers start asking us for help.

    Thank god i dont use MS products

  13. Glen says:

    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?

  14. Anon says:

    Yeah it will, save a ThreadAbortException

  15. Bob Gusek says:

    My vote is that it will display the message box.

    Even though you are returning the value, the Finally block is always executed.

    BTW, missed you at the MSDN event today in High Point, your substitute did do a great job though.

Comments are closed.

Skip to main content