Error and Exceptions handling in Visual Basic 2013 and resolution

Asked By 80 points N/A Posted on -
qa-featured

I am creating a program in VB that needs to show the proper usage of Error goto. How to use it? How can I implement it in any VB program? what are the codes that I need to include in implementing this? What are the best uses of this code? Is there any alternative that I can use for this? Why do I have to use this code? What are the benefits of showing the errors in my program? Will my program be better if I will show the error in real-time or just better to not show it?

Thank you

SHARE
Answered By 0 points N/A #191213

Error and Exceptions handling in Visual Basic 2013 and resolution

qa-featured

Hello Edith!

Here is the sample of and errorhandler in Visual Basic.

Example:

Line 1: Public Sub InitializeMatrix(ByVal Var1 As Object, ByVal Var2 As Object)

Line 2:    On Error GoTo ErrorHandler

Line 3:    ' Insert code that might generate an error here

Line 4:    Exit Sub

Line 5: ErrorHandler:

Line 6:    ….(code to handle error or post message to avoid program termination)

Line 7:   Resume Next

Line 8: End Sub

Explanation: (Please ignore lines 1 and 8 since it is predetermined in the code window.)

Line 3 will provide you option that will generate code. Whatever the problem and error the program you created will store the error to be generated. Line 6 must be implemented to show exactly what is the error generated and how to resolve it. There are other ways to handle simple error. Having null value in a textbox must provide us instructions that we need to put a value at least 1. Using if statement will be the best option.

Example:

Line 1:   If textbox1.text=”” Then

Line 2:                   MsgBox”Please Enter value!”

Line 3:   Else

Line 4:                   msgBox”<enter any information here”> and proceed to next command.

Line 5:   End If

Explanation: (Lines 1, 3 and 4 is the block code for If-Else Statement)

Line 2 will give you instructions that you have to enter information or a value to proceed to the next step of your program. Until you provide the required information you will not proceed to the next step. This is similar to the process of verifying any username and password in websites.

Error handling is very important and must be implemented to show interactivity and dynamic application. Plus this provide information that the user is doing the right thing before moving to the next step.

Related Questions