Solved

Vb.net On Error Resume Next & Error Checking

Posted on 2009-03-30
8
710 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:sgaggerj
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24020180
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
 
LVL 9

Expert Comment

by:Deathrace
ID: 24020253
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
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 125 total points
ID: 24020314
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 375 total points
ID: 24020749
@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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:sgaggerj
ID: 24020967
@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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 375 total points
ID: 24021057
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 24021097
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
 
LVL 2

Expert Comment

by:compdrmt
ID: 24469168
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now