Being a vb.noob as basically most of the vb.net world, I use the Try/Catch statement quite a bit for when it comes to errors, as the following.

        With Me
            .Text = "a"
            Try
                .Text += 1
            Catch ex As Exception
                MsgBox("error: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            MsgBox("a")
            .Text = "b"
            Try
                .Text += 1
            Catch ex As Exception
                MsgBox("error: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            MsgBox("b")
        End With

For the first time today, I attempted to use the On Error Resume Next and it returned the results I needed; less lines of code and only results if an error occured.

        On Error Resume Next
        With Me
            .Text = "a"
            .Text += 1
            MsgBox("a")
            .Text = "b"
            .Text += 1
            MsgBox("b")
        End With

My question is, even though quite clear to me:
For such lines of code as above, is the On Error Resume Next appropriate and can I move on to the next part of my.app with no worries, ever?
.thanx.in.adv,.Me.

Recommended Answers

All 6 Replies

By using a Try...Catch statement you can catch the error and do something about it.
It's a very useful debugging tool.

However, you don't have to enclose every single piece of altering code in it's own statement.
You can start the Try statement even before the line With Me and end it below the line End With, followed by the Catch statement with a messagebox displaying the error.

With On Error Resume Next, you won't know if an error occured and on what line.
So you have no way of knowing and thus can't do much about it.

Also.
Personally I feel that those types of error catching methods are depricated as they originate from the old-school VB type programming.
The Try...Catch statement is the .NET way.
That includes the use of MsgBox. Have you looked at MessageBox()?

Thanks for the informative reply Oxiegen. :)

I basically need to run a Sub that will set values to TextBoxes with Ctype(someCoolForm.Controls("someCoolTextBox"),TextBox).Text="daniweb.com" and wanted to use the minimum amount of typing/lines to accomplish this, even if it throws an error in case a TextBox does not exist on a Form; since planning to use that Sub for multiple.Forms/.Apps.
.Using a Try/Catch, it will error on first error and go straight to the Catch part, which does Nothing for the remaining TextBoxes waiting in line for values, If they exist on a specified Form.

As for the On Error Resume Next, I think that it should stay as a .Net option, even if integrated from the previous non.Net Visual Basics. A very strong opinion from my point of view.

Regarding the "Have you looked at MessageBox()?":
I have and it does offer a few/or one extra option(s) above the MsgBox, though for testing, MsgBox is less time typing and quicker results. :)

ON ERROR RESUME NEXT is a holdover from the days before TRY-CATCH. It is still used in vbScript where the TRY-CATCH is not available. It was the only way to prevent your program from crashing on an error and typically resulted in endless error checking tests (ugly) such as

on error resume next
avg = total / numitems

If err.Number <> 0 Then
    'error processing code
    err.Clear   ' maybe
End If

Reverend Jim, should I proceed with the On Error Resume Next or Not? Seems like the only clear.solution for this issue, other than a bunch of "GoTo" and tons of Try/Catches. AndAlso, thanx for code.sample, news2Me.

I've done a ton of vbScript (most of my apps at work from 2000-2008 were glue, data mining and system maintenance apps) and I used ON ERROR RESUME NEXT a lot. Most of my apps ran unattended and it was not acceptable for them to abort. However, I would not recommend it for VB apps. This may be against the general concensus but my recommendation is to use code to test for possible errors prior to doing an operation where possible and use TRY-CATCH sparingly. For example, if you are planning to do a numeric operation on the contents of a textbox, my preference would be to do something like

If IsNumeric(txtMycontrol.text) Then
    'do regular stuff
Else
    'do error stuff
End If

With regular expressions and such it is fairly simple to validate textboxes to ensure the text is valid. For operations that are more difficult to pre-validate (database operations, for example) I would use a TRY-CATCH.

But that's just my preference.

I went ahead with the On Error Resume Next and it met the requirements needed for me to accomplish my "previous" task.

Thanx for any and all input from the both of you; quite more informative than expected. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.