
|
Visual Basic automatically adds a crude form of error handling to your application. If an error occurs during a procedure your app will come with a message with error code and description. This is OK as a first 'line of defense' (some developers will not agree with this and argue that every procedure must have error handling).
Add Error HandlingYou can either add error handling using the builder to make a procedure, or you can add it afterwards using from the menu: Alt-CEA. Use Alt-CEA[down-arrow] to open the combo box to show the full list to select the preferred errorhandler from. When considering error handling we have to match the possible error situations with the available error handling options: 1. Simple property Let/Get situation. - no error handling
I am quite certain we don't need error handling here. What could go wrong with assigning a string to a string variable? |
|
If your code does nice things like changing the status bar or setting hourglasses or disabling controls that are not critical to the main process you may decide in case of error you don't want to bother the user with minor errors.
Supports a common technique of transferring control to section with code dedicated to handling the error:
If this is chosen additionally the module basErrorHandle with centralized error handling procedure ErrorHandle is inserted.
The well-known example here is of course trying to read a file. You may only hope it is there. There is a whole set of related and similar errors and handling them is probably something you want to do in the procedure in which the error occurs (e.g. retry/cancel), not in the centralized error handler.
OK, you probably know a better way. No problem, you can create your own error handling fragment using the Fragment Editor. As long as you store it in the \Fragments\ErrorHandlers subfolder it will automatically be added to the selection list.
We have added a simple form of centralized error handling in \vbcode\ErrorHandlers\basErrorHandle.bas. You may choose to change it or make an alternative handler module and refer to that in dialog Settings / Files and Folders.
It is a common practice to make available not only the error info, but also where the error occurred. For this automatically Module and procedure name strings are added if they are needed. Again the name of these strings can be customized in the Settings dialog. If you add line numbers you can even identify which line the error occurred.
To add line numbers:
To remove line numbers: