Solved

About "on error resume next" in VB6

Posted on 2000-02-26
11
536 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
[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
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 28

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 70

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

632 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