| Start |
|---|
| Avoiding error conditions When a runtime error occurs, good code should handle it. The best error handling strategy is to write code that checks for error conditions and simply avoids executing code that results in a runtime error. One key element in reducing runtime errors, is writing small procedures that do one thing. The fewer reasons procedures have to fail, the easier the code as a whole is to debug. Avoiding runtime error 91 - Object or With block variable not set: This error will be raised when an object is used before its reference is assigned. One might have a procedure that receives an object parameter: If If This way of early-exiting a procedure when one or more parameter isn't valid, is called a guard clause. Avoiding runtime error 9 - Subscript out of range: This error is raised when an array is accessed outside of its boundaries. Given an index greater than the number of worksheets in the Most runtime errors can be avoided by carefully verifying the values we're using before we use them, and branching on another execution path accordingly using a simple |
| Custom Errors Often when writing a specialized class, you'll want it to raise its own specific errors, and you'll want a clean way for user/calling code to handle these custom errors. A neat way to achieve this is by defining a dedicated Using the Raising your own runtime errorsA runtime error can be raised using the The And then create a dedicated private method to raise each error: The class' implementation can then simply call these specialized procedures to raise the error: The client code can then handle Note: the legacy |
| Resume keyword An error-handling subroutine will either:
The There are several ways an error-handling subroutine may use the
On Error Resume NextThe If the error isn't actually handled before doing that, then execution is permitted to continue with potentially invalid data, which may result in logical errors and unexpected behavior. The emphasis above cannot be emphasized enough. On Error Resume Next effectively ignores all errors and shoves them under the carpet. A program that blows up with a runtime error given invalid input is a better program than one that keeps running with unknown/unintended data - be it only because the bug is much more easily identifiable. The However sometimes an error condition can't quite be avoided, and jumping to an error-handling subroutine only to The |
| On Error statement Even with guard clauses, one cannot realistically always account for all possible error conditions that could be raised in the body of a procedure. The Line labels denote subroutines: because subroutines originate from legacy BASIC code and uses
This means a procedure that handles its errors, should be structured like this: Error Handling StrategiesSometimes you want to handle different errors with different actions. In that case you will inspect the global As a general guideline, consider turning on the error handling for entire subroutine or function, and handle all the errors that may occur within its scope. If you need to only handle errors in the small section section of the code -- turn error handling on and off a the same level: Line numbersVBA supports legacy-style (e.g. QBASIC) line numbers. The If you are using line numbers, but not consistently, then Keep in mind that The line number isn't quite as relevant as the statement that caused the error, and numbering lines quickly becomes tedious and not quite maintenance-friendly. |
Read Article
https://develop.horje.com/learn/1434/reference