Vb.net On Error Resume Next & Error Checking

I have a case in which i need to execute a sequence of statements and want to do some error handing.
i don't care if any fail, i don't care which one failed, but i need to know that one failed, and they all need to be executed

i was thinking of using ON ERROR RESUME NEXT, but I can't figure out how to trap an error if it occured.

i don't want to use try / catch because that will jump out and leave the rest of the code un-executed.

any ideas?

below is the code i need to execute, essentially the only error that can occur is if any one of the values is out of range for the allowable indecies.


Contacts_MiscNotes.Text = r.Item(Contacts.MiscNotes)
                Contacts_ContactCategory.SelectedIndex = CInt(r.Item(Contacts.CustomerCategory))
                Contacts_ContactType.SelectedIndex = CInt(r.Item(Contacts.CustomerType))
                Contacts_ContactAgent.SelectedIndex = CInt(r.Item(Contacts.AgentOf))
                Contacts_PaymentTerms.SelectedIndex = CInt(r.Item(Contacts.Terms))
                Contacts_StdDiscount.Text = r.Item(Contacts.StandardDiscount)
                Contacts_FoundVia.SelectedIndex = CInt(r.Item(Contacts.ContactFoundVia))

Open in new window

LVL 1
sgaggerjAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
@Deathrace: You can't mix the old style "On Error" with the newer "Try/Catch" block inside the SAME method.

Why not just set a boolean flag that gets toggled when an error occurs?  Then you can check it at the bottom?

    Private Sub Foo()
        Dim ErrorOcurred As Boolean = False
        On Error GoTo Oops

        ' ... bunch of code here...
        ' line1
        ' line2
        ' line3
        ' etc....

        If ErrorOcurred Then
            ' ... do something in here...
        End If
        Exit Sub

Oops:
        ErrorOcurred = True
        Resume Next
    End Sub
0
 
rockiroadsCommented:
well if u wrap this in a try/catch and in the exception handler, dump the stack trace, that gives you a line number. Would that help?

try
   ... your code
catch ex as Exception
    msgbox ex.stacktrace
end try
0
 
DeathraceCommented:
ON ERROR RESUME NEXT, remember the Finally block, it executes even your get any exception in your code block, try using that. Put the code which you need to execute even you get any exception in Finally block, the reason when ever you get any exception within the trycatch block, the control will definitly execute Finally block.

Might do some help... invent yourself
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
rockiroadsConnect With a Mentor Commented:
using on errror resume next, continue using old style code? not very elegant mind you

        On Error Resume Next
        Err.Clear()
        Contacts_MiscNotes.Text = r.Item(Contacts.MiscNotes)
        If Err.Number <> 0 Then
            MsgBox("Failed here 1")
            Err.Clear()
        End If
        Contacts_ContactCategory.SelectedIndex = CInt(r.Item(Contacts.CustomerCategory))
        If Err.Number <> 0 Then
            MsgBox("Failed here 2")
            Err.Clear()
        End If
        Contacts_ContactType.SelectedIndex = CInt(r.Item(Contacts.CustomerType))
        If Err.Number <> 0 Then
            MsgBox("Failed here 3")
            Err.Clear()
        End If

0
 
sgaggerjAuthor Commented:
@rockiroads,

your 1st example is what i don't want to do - i need all the statements to execute, whether they error or not.
i can't jump out and not execute the rest.

the 2nd example is more what i was looking for - however, i don't care which one errored out.  i just need to know that there was an error. doesn't matter where.

@idle_mind,

in your example, suppose i error on 'line 2'
it jumps to Oops, sets error occurred = true, then....
does the resume next, jump back and start from line 3?
(that's where i was lost)


0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Exactly..."Resume Next" means just that: Resume with the next line after where the error occurred.  =)


See: http://msdn.microsoft.com/en-us/library/91k6c8b5(VS.80).aspx

    "If the error occurred in the same procedure as the error handler, execution resumes with the statement immediately following the statement that caused the error."
0
 
rockiroadsCommented:
I realised after I posted that it will not continue code as its not good practice to have multiple try/catches also.
thats why i did the second example, on error resume next, will ignore the error but look out for error code and continue with next line. It is old style vb code, to be honest, I havent tried that approach in vb.net. Still seems to be available for backwards compatability (old converted vb code) probably
0
 
compdrmtCommented:
I  have used the accepted solution and iit works well but i wanted to add one thing.  if you are in a loop resume next can cause arror all day and nexer exit. your app will seem to hang   I change the oops to something like the following

dim errcnt as integer = 0
on error goto oops

Oops:
         ErrorOcurred = True
' I use some code to log the error to a db or a text file or both

 if errcnt < SomeReasonablyHighNumber then  
errcnt = errcnt + 1
        Resume Next
else
throw new exception("my own message here")
exit sub
end if

    End Sub

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.