VBA Error "Bubble Up"

I have not read much about this, but the author at the link below recommends that I do not use the "bubble" to centralize error handling in VBA.

Excel Weekend Programming Tutorial Through Google Books

But I'm not sure why he recommends this, and he really doesn't explain.

Can someone tell me why I should put error handling in EVERY procedure instead of using bubble up? Or at least do you know why the author is not speaking?

Thank.

+3
vba excel-vba
Sep 13 '09 at 20:33
source share
5 answers

I'm not sure what the default error handling is for VBA, but since its Visual Basic for applications and these applications include things like excel and word, I assume that only a dialog box will appear that will not be useful to the user.

I assume that the author was bitten by code that does not handle errors, so now he recommends all the procedures for handling errors.

The complete answer is that you must know every error that can occur and have code to handle it as much as possible (where you don't know what to do) or as high as possible (this means that when working with an error while processing the error code, but not knowing why the error occurred) either strategically (which is in the right places where you can recover most of the common errors) or just everywhere (which may be too much development effort).

0
Sep 13 '09 at 21:11
source share

A short answer to your first question: "you should not put an error handler in every procedure."

To say that “every procedure must have an error handler” is, in general, terrible advice. The disadvantages of handling VBA errors have been discussed elsewhere. Conceptually, however, this is not all that differs from the more standard form of exception handling found in other languages. Most of the best practices in these languages ​​apply. You should handle errors at the lowest level, where handling them makes sense. Sometimes this happens in the procedure where the error occurred, many times not.

For example, a VBA UDF called from your worksheet must necessarily have an EH that ensures that you return the Excel error value to the calling cell (s) instead of dropping the user into the code editor with the error message. However, the code you call from this UDF may not be needed. In fact, often the most significant thing an internal routine can do when an error occurs is simply to let it go through the stack so that it can reach code that knows what to do with it. It really depends on the routine.

The answer to your second question is that the author does not seem to be very good at handling exceptions. He admits that error handling is context-specific, but then it seems that each procedure should locally decide between "fix the problem right here and resume execution" and "end the program." He usually leaves the correct option, which is "cleared locally and raises the problem above." Thus, routines that do not need to be cleaned locally should simply make bubbles.

+11
Sep 13 '09 at 23:51
source share

My 2 cents: You must put error handlers in all public procedures and events. This means that the procedure at the bottom of the call stack will always have an error handler. Then add error handlers to your other routines, as that makes sense. If an error occurs in a procedure that does not have an error handler, it “bubbles up” with a top-level error handler, where it will be registered / displayed professionally. The scenario in which you might want to add an error handler for the private (lower level) procedure is as follows: The code should be fast. You have a rare condition that can be avoided, but will force you to perform an expensive logic test inside a loop (or, even worse, a nested loop). You can perform a logical test in the error handler, and if he said "rare occurrence", perform the correction and resume. Since this condition is rare, you will see performance gains for most conditions. If the error handler cannot identify and fix the problem, try again to put it on the stack.

Obviously, this is just one scenario.

+1
Sep 15 '09 at 1:26
source share

I see at least one reason in his explanation: because it deprives you of the opportunity to renew (the next one).
In addition, you will not know in which module an error occurred.

0
Sep 13 '09 at 20:43
source share

It is better not to use “bubble handling” in error handling, because errors must be processed, and if you know what to do , if such an error occurs, the procedure regarding what to do is better than the calling procedure .

Sub test() On Error GoTo e Dim c As Integer Dim d As Integer c = add(5, 0) d = divideWhichManagedItsOwnErrorHandling(5, 0) d = divide(5, 0) Exit Sub e: MsgBox "error occurred somewhere for which I don't know what to do: " + Err.Description End Sub Function add(a As Integer, b As Integer) As Integer add = a + b End Function Function divide(a As Integer, b As Integer) As Integer divide = a / b 'if error occurs, it will "bubble-up" to the caller. End Function Function divideWhichManagedItsOwnErrorHandling(a As Integer, b As Integer) As Integer On Error Resume Next Dim result As Integer result = a / b If Err.Number = 11 Then 'if divide by zero occurred, user must have passed 0 for b result = 0 ' return 0 if the divide by zero occurs. End If divideWhichManagedItsOwnErrorHandling = result End Function 
0
Sep 13 '09 at 20:59
source share



All Articles