School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,449 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,565 people online right now. Registration is fast and FREE... Join Now!




Error Handling in VB

 
Reply to this topicStart new topic

> Error Handling in VB, Definition and a Simple Example for beginners

Nikhil_07n
Group Icon



post 26 Jan, 2009 - 07:24 PM
Post #1


Intro

Error handling is an essential procedure in Visual Basic programming because it can help make the program error-free. An error-free program can run smoothly and efficiently, and the user does not have to face all sorts of problems such as program crash or system hang.
Errors often occur due to incorrect input from the user. For example, the user might make the mistake of attempting to ask the computer to divide a number by zero which will definitely cause system error. Another example is the user might enter a text (string) to a box that is designed to handle only numeric values such as the weight of a person, the computer will not be able to perform arithmetic calculation for text therefore will create an error. These errors are known as synchronous errors.

Therefore a good programmer should be more alert to the parts of program that could trigger errors and should write errors handling code to help the user in managing the errors. Writing errors handling code should be considered a good practice for Visual Basic programmers, so do try to finish a program fast by omitting the errors handling code. However, there should not be too many errors handling code in the program as it create problems for the programmer to maintain and troubleshoot the program later.

Writing the Errors Handling Code :

We shall now learn how to write errors handling code in Visual Basic. The syntax for errors handling is

On Error GoTo program_label

where program_label is the section of code that is designed by the programmer to handle the error committed by the user. Once an error is detected, the program will jump to the program_label section for error handling. It acts like a bookmark in VB

Example

Intro : This code shows how to handle error 'Division By Zero'.

CODE

Private Sub CmdCalculate_Click()

Dim firstNum, secondNum As Double
firstNum = Txt_FirstNumber.Text
secondNum = Txt_SecondNumber.Text
On Error GoTo error_handler
Lbl_Answer.Caption = firstNum / secondNum
Exit Sub        'To prevent error handling even the inputs are valid

error_handler:
Lbl_Answer.Caption = "Error"
Lbl_ErrorMsg.Visible = True
Lbl_ErrorMsg.Caption = " You attempt to divide a number by zero!Try again!"

End Sub


Explanation :

The line 'On Error GoTo error_handler' gets executed when there occurs an error in DIVISION.

When we input secondnum=0 then the line returns an error. then the 'error_handler' section of code is executed, simple.

NOTE : IT may be noticed that if there is no error , the error handling part of code is NOT executed due to use of EXIT SUB.

Was'nt that easy, handling errors?
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

bayron.jofel
*



post 12 Aug, 2009 - 08:42 AM
Post #2





thanks for the sample code,but how does it detect the error if the 'on error goto error_handler' line is written before the division process of two numbers?

This post has been edited by bayron.jofel: 12 Aug, 2009 - 08:44 AM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 01:48AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month