- Local time
- Today, 07:43
- Joined
- Feb 28, 2001
- Messages
- 30,566
I am not 100% sure about this, but if I correctly recall the rules of error handling, this sequence cannot work.
The part that bothers me is the ON ERROR RESUME NEXT within the error handler. The catch is that error handling routines cannot see errors in themselves. More specifically, an error trap goes to the first inactive but enabled error handler. In order to execute that ON ERROR RESUME NEXT in that position, you would have to already be in an active error handler. And that IS an executable statement, not a compile-time statement, because you can have sequences that do ON ERROR GOTO 0 followed by ON ERROR RESUME NEXT or ON ERROR GOTO NEW_HANDLER within a routine and they each take effect in proper sequence.
The other problem is this, and it isn't a terrible problem, its more of a "moot point" problem. In that error handler, you will always do nothing. If the flag is TRUE (blLogExists) then (from your comments) you will try to kill the log file. If you get the "Permission Denied" error then you exit the function at its "normal" exit point. But if the error is NOT #9, you fall through to an END FUNCTION - which is the other legal way to exit a handler. And if theblLogExists is FALSE then you also fall through to the END FUNCTION. In any case you do nothing (unless you put code in to delete the file and it actually DOES get deleted.) But as I pointed out above, if there IS an error, this handler will NOT perform the ON ERROR RESUME NEXT.
Then there is questionable wisdom of actually deleting a file from an Exit Handler. Recommended "best practices" for error handlers are to leave behind a trace of their findings, like setting a flag or something, that would then be detected by the main-line event code that had declared the handler.
You DO NOT WANT to slow down your code by engaging in a file operation from within an exit handler. You COULD, if you really wanted, test variables to decide whether some external test has verified your ability to take the desired action. But error handlers are like interrupt routines. Nothing in your code can interrupt an error handler so you are in that context until you RESUME out of the handler OR the first error that occurs inside that handler WILL leave that handler in the dust, since errors cannot interrupt active handlers. They can only stop them.
Code:
Errhandler:
if blLogExists then
On Error Resume Next
Now try to kill log file
If err.number=9 then 'permission denied
Resume ExitFunction
End if
The part that bothers me is the ON ERROR RESUME NEXT within the error handler. The catch is that error handling routines cannot see errors in themselves. More specifically, an error trap goes to the first inactive but enabled error handler. In order to execute that ON ERROR RESUME NEXT in that position, you would have to already be in an active error handler. And that IS an executable statement, not a compile-time statement, because you can have sequences that do ON ERROR GOTO 0 followed by ON ERROR RESUME NEXT or ON ERROR GOTO NEW_HANDLER within a routine and they each take effect in proper sequence.
The other problem is this, and it isn't a terrible problem, its more of a "moot point" problem. In that error handler, you will always do nothing. If the flag is TRUE (blLogExists) then (from your comments) you will try to kill the log file. If you get the "Permission Denied" error then you exit the function at its "normal" exit point. But if the error is NOT #9, you fall through to an END FUNCTION - which is the other legal way to exit a handler. And if theblLogExists is FALSE then you also fall through to the END FUNCTION. In any case you do nothing (unless you put code in to delete the file and it actually DOES get deleted.) But as I pointed out above, if there IS an error, this handler will NOT perform the ON ERROR RESUME NEXT.
Then there is questionable wisdom of actually deleting a file from an Exit Handler. Recommended "best practices" for error handlers are to leave behind a trace of their findings, like setting a flag or something, that would then be detected by the main-line event code that had declared the handler.
You DO NOT WANT to slow down your code by engaging in a file operation from within an exit handler. You COULD, if you really wanted, test variables to decide whether some external test has verified your ability to take the desired action. But error handlers are like interrupt routines. Nothing in your code can interrupt an error handler so you are in that context until you RESUME out of the handler OR the first error that occurs inside that handler WILL leave that handler in the dust, since errors cannot interrupt active handlers. They can only stop them.