On Error Resume Next

G

Guest

If I use "On Error Resume Next" in a public function, does "On Error Resume
Next" remain in effect after stepping out from the function, or have I to
explicitely make it ineffective with an "On Error Goto 0" statement before
steeping out from the function?
Thanks,
Stefi
 
R

Rob Bovey

Stefi said:
If I use "On Error Resume Next" in a public function, does "On Error
Resume
Next" remain in effect after stepping out from the function, or have I to
explicitely make it ineffective with an "On Error Goto 0" statement before
steeping out from the function?

Hi Stefi,

The effect of On Error Resume Next is generally confined to the
procedure where it is used. Once code exits that procedure it has the same
effect as the On Error Goto 0 statement. The one major exception to this
rule is when you call another procedure that has no error handling of its
own after an On Error Resume Next statement. In this case, an error in the
called procedure (the one with no error handling) will cause code execution
to silently bail out of that procedure and continue on the next line in the
calling procedure (the one where On Error Resume Next is in operation).

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm
 
G

Guest

Thanks Rob, it's clear. But the exception you mentioned is not really an
exception, because the code did not exit the procedure with the "On Error
Resume Next" in operation, so the original rule prevails: "On Error Resume
Next" remains in effect until exiting the procedure it was used in, OR the
code execution reaches another "On Error" statement that explicitly override
the original "On Error Resume Next".
Am I right?

Regards,
Stefi

„Rob Bovey†ezt írta:
 
R

Rob Bovey

Stefi said:
Thanks Rob, it's clear. But the exception you mentioned is not really an
exception, because the code did not exit the procedure with the "On Error
Resume Next" in operation, so the original rule prevails: "On Error Resume
Next" remains in effect until exiting the procedure it was used in, OR the
code execution reaches another "On Error" statement that explicitly
override
the original "On Error Resume Next".
Am I right?

Hi Stefi,

Yes, you are correct..

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm
 
T

Tushar Mehta

Ummm...kinda. The error trapping remains effective but in the scope of
the *sub that set it.* So, as Rob mentioned in his first post, control
immediately returns to the next statement in the *calling* procedure.
To illustrate:

Sub SetOnError()
Dim y
On Error Resume Next
CalledRoutine y
MsgBox y
End Sub
Sub CalledRoutine(ByRef x)
x = 1 / 0
x = x + 1
End Sub

The x=x+1 statement is *not* executed, the sequence of statements
being:
enable error trapping
call CalledRoutine
divide 1 by zero
resume with Msgbox statement in calling procedure.

On the other hand, if you added error handling to CalledRoutine a la

Sub CalledRoutine(ByRef x)
On Error Resume Next
x = 1 / 0
x = x + 1
End Sub

The MsgBox would show 1.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
G

Guest

Hi Tushar,

That doesn't conflict at all what I wrote:

In your example 1
"On Error Resume Next" remained effective in the scope of the *sub that set
it* - including procedure calling (CalledRoutine y) -, because the code did
not exit the procedure the "On Error Resume Next" was set in.

In your example 2
"On Error Resume Next" set in the calling sub didn't remain effective,
because the code execution reached another "On Error" statement that
explicitly overrode
the original "On Error Resume Next".

Regards,
Stefi

„Tushar Mehta†ezt írta:
 
T

Tushar Mehta

{shrug}
This wasn't meant to be a war of semantics.

If you are happy with your interpretation of how error handling works,
good for you.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top