Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

About "on error resume next" in VB6

Posted on 2000-02-26
11
Medium Priority
?
544 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
Industry Leaders: 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 150 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

715 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