Solved

About "on error resume next" in VB6

Posted on 2000-02-26
11
523 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 121
How to debug this code 7 61
Excel VBA, find a string in a column, update a cell 7 81
Recommendation vb6 to vb.net or others 14 150
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

803 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