Click here to Skip to main content
12,511,920 members (47,676 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked

VB .NET Error Handling

, 2 Apr 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Introductory tutorial on Visual Basic error handling


In this article, you’ll learn how to build blocks of code that handle run time errors, also referred as exceptions which occur as a result of normal operating conditions for example. Errors due to a disk not being in the drive or to an offline printer.

Visual Basic .NET includes the Try---Catch code block, a new syntax for handling error. In this article you’ll learn how to trap run time errors using Try---Catch code block, and you’ll learn how to use the Err.Number and Err.Description properties to identify specific runtime errors. You’ll also learn how to use multiple Catch statements to write more flexible error handlers, build nested Try---Catch code blocks, and use the Exit Try statement to exit a Try --- Catch code block prematurely.


2.Processing Errors Using Try - - - Catch:

A runtime error, or program crash, is an unexpected problem in a Visual Basic program from which the program can’t recover. It’s not that Visual Basic don’t smart enough to handle the glitch; it’s just that Visual Basic hasn’t been told what to do when something goes wrong.

Fortunately you don’t have to live with the occasional errors that cause your programs to crash. You can write special Visual Basic runtimes, called structured error handlers, to respond to runtime errors. An error handler handles a runtime error by telling the program how to continue when one of its statements doesn’t work. Error handlers are placed in the event procedures, in which there is a potential for trouble, or in generic functions or subprograms that handle errors for you systematically. As their name implies, error handlers handle, or trap, a problem by using the Try --- Catch statements and a special error handling object Err. The Err object has a Number property that identifies the error number and a Description property that allows you to print a description of the error.

3.When to Use Error Handlers:

You can use error handlers in any situation in which an expected or an unexpected action might result in an error that stops program execution. Typically, error handlers are used to process external events that influence a program – for example, events caused by a failed network or Internet connection, a disk not being in the floppy drive, or an offline printer. The Following table lists potential problems that can be addressed by error handlers



Network/Internet Problems

Network servers, modems or resources that fail, or go down, unexpectedly.

Disk drive problems

Unformatted or incorrectly formatted disks, disk that aren’t properly inserted, bad disk sectors, disks that are full, problems with CD-ROM drives, and so on.

Path Problems

A path to a necessary file is missing or incorrect.

Printer Problems

Printers that are offline, out of paper, out of memory, or otherwise unavailable.

Software not installed

A file or component that your application relies on is not installed on user’s computer, or there’s an operating system incompatibility.

Permission Problems

The user doesn’t have the appropriate permissions to perform a task.

Overflow Errors

An activity that exceeds the allocated memory space.

Out-of-Memory errors

Application or resource space that’s not available in Microsoft Windows.

Clipboard Problems

Problems with data transfer or Windows clipboard.

Logic Errors

Syntax or logic error undetected by the compiler


4.The Try --- Catch Statement:

The basic syntax for Try - - - Catch exception handler is simply the following:

The Try Statement identifies the beginning of an error handler:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ujwal Watgule
Team Leader
India India
Hello Reader, This is Ujwal working with a consultancy firm. I develope applications in Microsoft .NET Technologies.

You may also be interested in...


Comments and Discussions

Questionerror handling in search using searc button Pin
Iswandi Abdul Rahman26-Aug-11 15:36
memberIswandi Abdul Rahman26-Aug-11 15:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 2 Apr 2006
Article Copyright 2006 by Ujwal Watgule
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid