Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Resume without error" error... 1

Status
Not open for further replies.

billybobk

Programmer
Oct 14, 2002
130
US
With this code:
====================================
ErrorHandler:
Select Case Err.Number
Case 9 ' "subscript out of range" error.
Exit Sub
Case Else
Resume Next
End Select
Resume
====================================
both the 'Resume next' and the 'Resume' raise the error ("Resume without error") -- but only sometimes! Any ideas why this would be? And What's wrong with Resume?
Thanks in advance..

--Bill
One may not reach the dawn save by the path of the night
--Kahlil Gibran
 
BillyBobK

Do you have an Exit Sub just before the ErrorHandler Code?

If not then your code is running into the handler like it was normal.

regards.
 
No I don't. Are you suggesting that I put an Exit Sub before the ErrorHandler code snippet above? I haven't seen that before..

--Bill
One may not reach the dawn save by the path of the night
--Kahlil Gibran
 
Thats precisely what you need to do.

If you don't then the code will just fall over.

Basically whenever you have an Error handling code at the end of a sub/function you need to place an exit sub/function immediately before it.

sub aa
on error to to errorhandler
call aa

exit sub

errorhandler:
Select Case Err.Number
Case 9 ' "subscript out of range" error.
Exit Sub
Case Else
Resume Next
End Select
Resume
end sub


Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Well thank you to both! I never knew! That would explain much wierd behavior and stuff I've observed in the past. It also means, oh damn, I've got to go through the code in all my apps I've written, find all the error handlers, and insert the 'exit subs'. Yow!

--Bill
One may not reach the dawn save by the path of the night
--Kahlil Gibran
 
I don't see how your code could have worked without them :)

Transcend
[gorgeous]
 
Um, well, gee, I guess as long as I didn't use those subs......!!

--Bill
One may not reach the dawn save by the path of the night
--Kahlil Gibran
 
Billybob,

I wouldn't worry too much about it buddy.
That is what this forum is for after all!

If you never asked, then you'd never know!
(Probably a lie.. Sure you'd have found out sooner or later)

Happy Coding!
 
If you need to release object references or otherwise clean up before exiting a sub then use a label below the handler. This way the clean up code always runs no matter if the sub ends normally or terminates with an error.

Sub aa

On Error GoTo errorhandler

'code here....

GoTo ExitSub

errorhandler:
Select Case Err.Number
Case 9 ' "subscript out of range" error.
Resume ExitSub
Case Else
Resume Next
End Select

ExitSub:
'Clean up code here

End Sub


Paul Bent
Northwind IT Systems
 
Very interesting. Thanks, Paul.

--Bill
One may not reach the dawn save by the path of the night
--Kahlil Gibran
 
Paul,

Can you do me a favour and clarify your meaning of 'Clean Up Code.'
(this is where I go into dopey mode...)
I don't use 'clean up code' (this is largely reasoned by the fact i don't know what it is)
How would you go about cleaning your code?

Thank you..
 
I think it's more about tidying up than cleaning! For example

Close any opened connections
Save changed registry settings (Form position/preferences)
Close and release external objects (eg Word/Outlook)
etc
etc

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Hello All,

Cant we write the Clean Up Code in the Error Handler itself ??

Good luck

Vikram
 
Yoshi,

johnwm has explained it very concisely!

vikram28,

You could but execution wouldn't normally go through the handler. The clean up code would also have to be duplicated in the normal path of execution. Not the best design.

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top