Learn the basics about structured exception handling in VB.NET
Takeaway: Irina Medvinskaya provides information, as well as code samples, about both types of structured exception handling blocks: Try…Catch and Try…Finally.
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.
Try…Catch
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:
Try
'Some code
Catch
'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.
Try…Finally
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
Try
'Use the resource
Finally
'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!
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- Email Security and Archiving - Clearer in the Cloud Google The time is NOW for businesses and organizations of all sizes to implement ... Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- Building the Virtualized Enterprise with VMware Infrastructure VMware This paper explains how adopting a virtual infrastructure -- comprised of server, storage, and networking virtualization technologies -- can help your organization build a sustainable competitive ... Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

