Error folding processing for the main procedure

If I have a basic procedure with error handling that calls other procedures without error handling, should the errors roll back to the main procedure?

What if error-free routines use error-free functions?

Should everything collapse before processing the main procedure?

+4
source share
3 answers

Yes, unhandled errors will collapse the stack. If you don’t have error handling in your main routine (or in the current event processing procedure), then the error will be minimized to VBA itself, which will either interrupt your program or reset the VBA environment within the host application. (you do not want this).


I can imagine two obvious exceptions to this: one illusory and one real:

1) If VBA is entered through an unexpected event path, instead of the main procedure, it may seem that the Main-routines error handler bypasses with a return error, but in fact it is a different thread, so when it is minimized from the event handler, it goes to VBA regardless main thread of the main program.

2) VBA error handling cannot catch ALL errors, in particular, most FATAL errors cannot be caught by it and the failure (and reset) of the entire VBA environment. Examples of errors.

+7
source

Error handling procedure A will catch all errors. Procedure B will not catch without error handling.

If A calls B and B has an error, it will be minimized and will go to the handler.
If B calls A and B has an error, there will be no processing.
If B calls A and A has an error, A will catch it.

No matter which function or procedure is irrelevant. However, it is worth noting that the class module will not handle errors locally, but passes them back to the stack for the first non-class module.

I'm not sure what you mean by should, but where you want the caught error to depend on what you want to do with the error. For example, if for a certain error you need to make a small adjustment for the line, and then resume it, then you need very local processing. In other cases, simplicity may require top-level processing.

+2
source

Errors that are not processed locally will always * fall into the trap of the up-the-stack error handler if the error capture parameter is set to "Break on unprocessed errors." You will find this option in the IDE's Options section.

If it is set to "Break on All Errors", an error handler (neither local nor top stacks) will be called. If it is set to "Break in Class Module", an error handler outside the class will not be called.

Note that this parameter changes the line on which the debugger stops: when an unhandled error occurs in the class module, the debugger stops in the line that calls the class using "Break on Unhandled Errors" or on the class violation line with the "Class break module" .

In Access, this option can be read programmatically using Application.GetOption ("Error Trap") and Application.SetOption ("Error Trap").

* As RBarryYoung wrote, the most fatal errors (for example, errors) will not be fixed by VBA error handling.

+1
source

All Articles