Solved

About "on error resume next" in VB6

Posted on 2000-02-26
11
520 Views
Last Modified: 2010-05-02
VB Code like this:

Private Sub Test()
On Error goto Errhandler

i=1
j=2
...(Some code that will run error)
exit sub
Errhandler:
  on error resume next
  i=1
  ...(Some code that will run error)
  j=2
End sub

In test(), when it run error, it
will goto ErrHandler, and then
it will run the code in Errhandler.
but if the program run error in
Errhandler, how to let it run the next
line of code in errhandler(Resume next)?

the code "On error resume next" in
Errhandler does not work correctly
when it run error in Errhandler.
Why?


0
Comment
Question by:hxb
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 2560654
Hi
Try this
Private Sub Test()
On Error Resume Next

i=1
j=2
....(Some code that will run error)
If Err then GoTo ErrHandler
exit sub
Errhandler:
'  on error resume next
  i=1
  ...(Some code that will run error)
  j=2
End sub


0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2560933
If errors occur in error handling routine, the error is automatically raised to the calling procedures.
0
 
LVL 12

Expert Comment

by:roverm
ID: 2561010
If I understand correct you want to continue after the error has been handled:

Private Sub Test()
On Error goto Errhandler

i=1
j=2
....(Some code that will run error)
exit sub
Errhandler:
'forget this line
'  on error resume next
  i=1
  ...(Some code that will run error)
'add this
  Resume Next
  j=2
  Resume Next
End sub

D'Mzzl!
RoverM
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2561117
From help:

"An 'enabled' error handler is one that is turned on by an On Error statement; an "active" error handler is an enabled handler that is in the process of handling an error. If an error occurs while an error handler is active (between the occurrence of the error and a Resume, Exit Sub, Exit Function, or Exit Property statement), the current procedure's error handler can't handle the error."

    i=1
    j=2

    On Error GoTo ERRHANDLER
    ...(Some code that will run error)
    ... More code (Resume next entered here)
Exit Sub
ERRHANDLER:
    'No errors will be handled here
    'If another error is raised, program crashes.
    Resume 'Loops code back to line causing error
    Resume Next 'Loops code to line after error line.
    Exit Sub
End Sub
0
 
LVL 12

Expert Comment

by:roverm
ID: 2562346
Erick37: Isn't that what I described?
Further: you should know better than to lock the question by 'answering it' when somebody else has placed a comment!
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 32

Expert Comment

by:Erick37
ID: 2562515
Hi Roverm:

In your code example you have:

Errhandler:
    'forget this line
    'on error resume next
    i=1
    ...(Some code that will run error)
    'add this
    Resume Next
    j=2
    Resume Next
End Sub

which is incorrect.  Firstly, the error in the Errhandler will not be handled.  Secondly, the Resume next line brings the code out of the error handler back to the line just after line which caused the first error.  The code

j=2
Resume Next

is never reached.

This is why I locked the question with an answer.
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 50 total points
ID: 2562911
On Error Goto ErrorHandler

A statement here that causes an error


etc...


ErrorHandler:

'Like Erick said, you're now in an active error handler. Get out to re-enable the error handling as you know it
Resume GetOutOfHere:
GetOutOfHere:
'Now error handling will behave as before
On Error Resume Next

Do something that might cause an error here.

0
 

Author Comment

by:hxb
ID: 2563299
caraf_g's code is good.

but how to give point to caraf_g?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2563656
Erick37 changed the proposed answer to a comment
0
 
LVL 12

Expert Comment

by:roverm
ID: 2563945
Erick37: Ok! ;-)) You're right, I forgot to remove the On Error Resume Next in the err handling routine !
0
 

Author Comment

by:hxb
ID: 2563977
Thank all experts.
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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

914 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

18 Experts available now in Live!

Get 1:1 Help Now