Solved

About "on error resume next" in VB6

Posted on 2000-02-26
11
519 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now