Finally Blocks

Much of this chapter has focused on how to write Try statements that contain Catch blocks. As you know, a Catch block is a piece of code that executes conditionally when things go wrong. A Try statement can also contain another construct known as a Finally block:

Try
  '*** Try block
Finally
  '*** Finally block
End Try

Unlike a Catch block, a Finally block doesn't execute conditionally when things go wrong. Rather, it executes unconditionally regardless of whether the Try block experiences an exception. In other words, the code within a Finally block executes in all situations.

A Finally block serves a different purpose than a Catch block. Because its code always executes, a Finally block is a great place for cleaning up and releasing ...

Get Building Applications and Components with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.