McBasic™Get McBasic™ ↗
Build 881
Language reference

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 causeWhy it happensRepair
Array is fixedThe 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 lockedAn 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#

LanguageWorkbench / Main.bas · CatchFailure
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 Function

The 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#

NumberTypical causeUseful next action
5Invalid argument or parameter contractCheck range, argument count and parameter names.
6Numeric overflowChoose a suitable type or constrain input.
9Invalid indexRead the collection or array’s actual bounds.
10Fixed array or temporary array lockRead which cause the message names; change the declaration or finish the borrowing call/loop.
340Missing control-array memberUse an existing Index; gaps between the bounds are allowed.
730Control name not foundCheck the explicit name passed to Me.Controls.
13Type mismatchInspect the value before converting it.
17Database operation failed or invalid database stateInspect SQL, schema and transaction ownership.
94Invalid use of NullUse 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.

Source baseline 20f4bfed6b · Build manifest
Sources & maintenance

Original web explanation, reviewed against these repository sources. API tables and example files follow the implementation.

  • docs/manual/21-language-reference.md
  • docs/manual/11-language-toolkit.md
  • site/examples/LanguageWorkbench/Main.bas
  • src/McBasic.Compiler/ArrayResizeAnalysis.cs
  • src/McBasic.Compiler/Binder.cs
  • src/McBasic.Compiler/CallArguments.cs
  • src/McBasic.Runtime/BasicRuntime.cs
  • site/examples/LanguageWorkbench/LanguageTests.bas