what is the point of 'Finally'

M

Mike W

I've been looking at some code and started wondering what is the point of
'Finally" in Try Catch Finally.
Why not just place the Finally code after the End Try? Will the Catch fire
if there is an error in Finally?

Try
Dosomething()

Catch ex as Exception
messagebox.show(ex.message)

Finally
CompleteProcess()

End Try

---------------------------------

Try
Dosomething()

Catch ex as exception
messagebox.show(ex.message)

End Try

CompleteProcess()

----------------------------------

What's the difference?

Thanks,
m
 
M

Marina

This has been asked many times before.

If the try or catch block is going to have a Return statement, then you
would have to place duplicate code in both the Try and Catch to execute
right before the Return. And if the Try has multiple return statements, you
would have to copy that code right before each one.

Additionally, you may not have a Catch block at all. But you may want code
to execute regardless of whether or not the Try block completed
successfully. So you can put that in the Finally, and not have any Catch
blocks at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top