Solved

Vb.net On Error Resume Next & Error Checking

Posted on 2009-03-30
8
718 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 92
More on Time zones in vb 2010 12 38
Help with AsEnumerable(), LINQ 4 39
Disable extension 8 38
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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

772 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