Software Development

Learn the basics about structured exception handling in VB.NET

Irina Medvinskaya provides information, as well as code samples, about both types of structured exception handling blocks: <i>Try&hellip;Catch</i> and <i>Try&hellip;Finally</i>.

One of the most reputable changes in the Visual Basic language is the introduction of structured exception handling in VB.NET. While the latest version of the language still supports the On Error Goto type of error handling, it's not preferred; instead, you should use structured exception handling.

VB.NET now supports the Try…Catch exception blocks and Try…Finally resource protection blocks. Here is more information about both types of structured exception handling blocks, along with code samples.


The Try…Catch block allows you to catch and handle errors for which the developer can specify a resolution. The basic format of the block is:

 'Some code
 'Error resolution whenever an error takes place
End Catch

The protected code appears in the Try section of the code, and the error resolution appears in the Catch section of the code. The Try code always gets executed, but the Catch code only gets executed if an error occurs.


The Try…Finally blocks are usually used in order to ensure that allocated resources are being cleaned up. These blocks allow you to catch and handle errors, as well as execute a section of the code regardless of whether there is an error. The basic format of the block is listed below:

'Resource allocation code
 'Use the resource
 'Clean the resource up
End Catch

The protected code appears in the Try section, and the clean up code appears in the Finally section. The statements in a Finally block are always executed when control leaves a try statement, regardless of whether there is an error in the execution.

Note: In real applications, you will often need to combine or nest the Try…Catch and Try…Finally blocks to design a more flexible error handling routine.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!


i want to know About Fornmats of Exception handling.. like 1)Does not throw exception 2)throw exception without catch 3)throw exception without catch 4)throw exception catch rethrow


Though both Microsoft documentation and this article states that the old ON ERROR is not preferred, I have found several times where I have needed the old way. There have been instances where I simply want errors ignored (and yes, I know what I'm doing). As far as I can tell, the new structured methodology will not allow this, w/o possibly putting a separate Try/Catch block around each statement, which I do think is preposterous. In general I do use (and like) the new method; I just think the scenario stated needs to be addressed before the old way is eliminated.


A "Try" structure should be closed with "End Try" not with "End Catch" as shown above.

Editor's Picks