Page 1 of 1

Error Handling in VB Definition and a Simple Example for beginners Rate Topic: -----

#1 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Post icon  Posted 26 January 2009 - 08:24 PM

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'.

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?

Is This A Good Question/Topic? 2
  • +

Replies To: Error Handling in VB

#2 bayron.jofel  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 16-April 09

Posted 12 August 2009 - 09:42 AM

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 August 2009 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Posted 25 November 2012 - 09:00 AM

View Postbayron.jofel, on 12 August 2009 - 04:42 PM, said:

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 simply happens because, when the program stumbles upon an error it stops, and doesn't go to the next line to check if you have any kind of error handling. Instead if you specify it before it encounters an error it knows how to handle it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1