?
Solved

Vb.net On Error Resume Next & Error Checking

Posted on 2009-03-30
8
Medium Priority
?
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

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