Solved

Vb.net On Error Resume Next & Error Checking

Posted on 2009-03-30
8
714 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

13 Experts available now in Live!

Get 1:1 Help Now