Computersight > Programming > Visual Basic

Error Handling in Vb.net

Two common ways to handle errors in your VB net program.

There are many ways to handle errors in vb.net. Although common and able to capture most errors I recommend you make your own error classes that could be built of these.

On Error

This is the easiest error catching to use and has 2 different ways of treating errors.

           A. On Error Resume Next      (The most common way just skips over the code that cause errors in your program. Not recommended because it could skip code that is important to the functioning of your program and not good for debugging.)

Ex.

On Error Resume Next  'This will last for the whole sub or function in which it is used in.
        Dim p As Integer  
        p = 100
        p = p / 0               'Since you can not divide by 0 this line of code will not be executed

           B. On Error Goto (sub name here)     (A better way of managing your errors this allows you to create your own sub to deal with the error or terminate your program safely.)

Ex.

On Error GoTo err1 'This will last the whole sub or until the error sub is hit
        Dim p As Integer
        p = 100
        p = p / 0          'This code will cause the err1 sub to execute
        Exit Sub           'Must have this or your error sub will execute
err1:
        MsgBox("Error closing program") 'Report the error to the user
        Me.Close()                      'close the program

Try End Try

A better way to catch errors as you can acually find out the error that occured and make a seperate function to handle it instead of one general one that we had to do above.

           A. Try Catch   (As Its name implies it will try the code and catch any error and execute any code under the Catch code.)

Ex.

Try
            Dim p As Integer
            p = 100
            p = p / 0           'If an error is generated the Catch code is called
            p = 75              'This code will not be executed
   Catch ex As Exception   'Code after this will only occur if there is an error
            MsgBox(ex.Message)  'If an error occurs a message will appear with some information on the error
End Try                 'End of the try code anything past here will not be checked for errors

            B. Try Catch Finally    (Same as Try Catch, but the Finally allows a place for the code to always execute even if there is an error.)

Ex.

Try
            Dim p As Integer
            p = 100
            p = p / 0 'Error occures here
    Catch ex As Exception
            MsgBox(ex.Message) 'This will execute after the error
    Finally 'The finally will execute after the catch code
            'Any code here will execute if there is an error
End Try

0
Liked It
I Like It!
Related Articles
Prohibit Users From Accepting Data in VB and Vb.net  |  Using VB.net for the First Time
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.