Errors, cleanup & diagnostics
Separate compiler diagnostics from runtime failures and preserve the operation’s cleanup path.
Check cannot predict every runtime result#
An undeclared variable or wrong event signature is a diagnostic before execution. A service failure, invalid converted value or unavailable file occurs while the program runs. Save the diagnostic code and source location for the first case; handle a useful error number and message for the second.
Distinguish a fixed array from a temporary lock#
| Message cause | Why it happens | Repair |
|---|---|---|
| Array is fixed | The variable was declared with bounds; ReDim or replacing the whole array would change its storage. | Declare an empty-parentheses dynamic array if resizing is intended, and ReDim it before the first element access. Keep a fixed declaration when only elements should change. |
| Array is temporarily locked | An element is borrowed ByRef or a For Each is enumerating the array. | Wait until the call or loop ends before resizing, erasing a dynamic array or replacing its contents. Changing the declaration does not release a lock. |
Both causes retain error number 10, but from build 833 the message identifies the cause and remedy. Build 834 additionally catches known fixed-array calls to resizing procedures during Check (MCB2102), including forwarding through other procedures. The runtime guard still protects cases that Check cannot prove. Practice the five-step declaration repair.
Follow success, failure and cleanup together#
Public Function CatchFailure() As Long
Try
Err.Raise 513, "LanguageWorkbench", "A deliberate practice error."
Catch failure As McException
CatchFailure = failure.Number
Finally
Debug.Print "Error cleanup completed"
End Try
End FunctionThe function deliberately raises error 513 and returns that number from Catch. Finally prints a cleanup message. Keep the entire block: copying only the success branch removes the part that makes the operation recoverable.
Common value and database errors#
| Number | Typical cause | Useful next action |
|---|---|---|
| 5 | Invalid argument or parameter contract | Check range, argument count and parameter names. |
| 6 | Numeric overflow | Choose a suitable type or constrain input. |
| 9 | Invalid index | Read the collection or array’s actual bounds. |
| 10 | Fixed array or temporary array lock | Read which cause the message names; change the declaration or finish the borrowing call/loop. |
| 340 | Missing control-array member | Use an existing Index; gaps between the bounds are allowed. |
| 730 | Control name not found | Check the explicit name passed to Me.Controls. |
| 13 | Type mismatch | Inspect the value before converting it. |
| 17 | Database operation failed or invalid database state | Inspect SQL, schema and transaction ownership. |
| 94 | Invalid use of Null | Use IsNull before converting a nullable value. |
Keep a domain error separate from the service error it follows. “There is no matching customer” is not the same as “the query could not execute”.
Put recovery beside resource acquisition#
Pair disabling a command with restoring it in Finally. Pair opening a transaction with commit, rollback or disposal. A failure must not leave a Form permanently busy or a transaction blocking the connection.
Make failure part of acceptance#
Exercise invalid input, a missing record, a cancelled operation and repeated activation. For UI operations, confirm that buttons become usable again and previous valid data remains understandable. See transaction rollback and download cancellation.
20f4bfed6b · Build manifestSources & maintenance
Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.
docs/manual/21-language-reference.mddocs/manual/11-language-toolkit.mdsite/examples/LanguageWorkbench/Main.bassrc/McBasic.Compiler/ArrayResizeAnalysis.cssrc/McBasic.Compiler/Binder.cssrc/McBasic.Compiler/CallArguments.cssrc/McBasic.Runtime/BasicRuntime.cssite/examples/LanguageWorkbench/LanguageTests.bas