VB .NET Error Handling

Introduction

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

In a real program you’ll probably want to use the Finally code block to update important variables or properties, display data, or perform other cleanup operations.

5.More Complex Try --- Catch Error Handlers:

As your program become more sophisticated, you might find it useful to write more complex Try --_ Catch error handlers that manage a variety of runtime errors and unusual error handling situations.Try --- Catch provides for this complexity by:

(3). Allowing nested Try --- Catch code block, which can be used to build sophisticated and robust error handlers.

In addition a special error handling object named Err allows you to identify and process runtime errors and conditions in your program.

6.The Err Object:

Err is a special Visual Basic object that’s assigned detailed error handling information each time a runtime error occurs. The mostuseful Err properties for identifying runtime errors are Err.Number and Err.Description.

Err.Number: contains the number of the most recent runtime error.

Err.Description: a short error message that matches the runtime error numbers.

By using the Err.Number and Err.Description properties together in an error handler, you can recognize specific errors and respond to them and you can give the user helpful information about how he or she should respond.

The Following table lists many of the runtime errors that Visual Basic application can encounter:

Error Number

Default Error Message

5

Procedure call or argument is not valid

6

Overflow

7

Out of Memory

11

Division by Zero

51

Internal Error

52

Bad file name or number

53

File not found

55

File already open

76

Path not found

482

Printer error

For more information on a particular error, search for Visual Studio online Help. Unused error numbers in the range of 1-1000 are reserved for future use by Visual Basic.NET.

This If---Then statement isn’t an actual error handler because it doesn’t prevent a runtime error from halting a program. Instead, it’s a validation technique that some programmers call Defensive Programming.

In this particular case, testing to see whether a file exists with a .NET framework method is actually faster than waiting for Visual Basic to issue an exception and recover from an runtime error using an error handler. When should you use defensive programming techniques and when should you use error handlers? The answer depends on how often you think a problem will occur with the statements you plan to use. If there’s a real likelihood that a piece of code will produce a runtime error more than 25 percent time, defensive programming logic is usually the most efficient way to manage potential problems.

Comments and Discussions

Hai ,i need advice and help here,In this programme i use 1 button( i name it btnsearch) and 1 textbox(i named it textboxname).The function of this button is use to search data in database and function of textbox is for user fill name. I want to put an error handling which if thefunction cannot search the data in database then it will appear message box inform that info not found. Here is my code.I am using visual studio 2005 & sql 2005 as database .