-
Notifications
You must be signed in to change notification settings - Fork 0
try catch
ffredyk edited this page Jul 23, 2026
·
1 revision
Compiler Construct (Error Handling)
try/catch are compiler-level constructs for structured error handling. Code in the try block executes normally. If a throw occurs, execution jumps immediately to the catch block with the thrown value available as _exception.
The compiler emits special opcodes for try/catch regions. The VM maintains an exception handler stack. When throw executes, the VM unwinds to the nearest enclosing try block and transfers control to its catch clause.
try {
if (_hp <= 0) then { throw "Unit is dead"; };
doWork();
} catch {
systemChat f"Error: {_exception}";
};try {
_result = riskyOperation();
} catch {
_error = _exception; // SqError object
diag_log f"Error at {_error.file}:{_error.line}";
_result = nil; // fallback value
};try {
try {
throw "inner error";
} catch {
systemChat f"Inner: {_exception}";
throw "outer error"; // re-throw
};
} catch {
systemChat f"Outer: {_exception}";
};_validate = {
params ["_val", "_min", "_max"];
if (_val < _min || _val > _max) then {
throw f"Value {_val} out of range [{_min}, {_max}]";
};
};
try {
[150, 0, 100] call _validate;
} catch {
systemChat str _exception; // "Value 150 out of range [0, 100]"
};Inside catch, the magic variable _exception holds the thrown value. It is an SqError object with:
-
.message— the error message string -
.file— source file path -
.line— line number -
.col— column number
try {
throw "Something broke";
} catch {
systemChat _exception.message; // "Something broke"
systemChat f"At {_exception.file}:{_exception.line}";
};Try/catch unwinds the current fiber only. Other fibers are unaffected.
- Getting Started
- Language Guide
- Type System
- Control Flow
- Functions & Code
- Strings & Text
- Arrays & Collections
- Concurrency
- Promise System
- Thread Safety
- Host API & Embedding
- CLI Tool
- Bytecode Reference
- Multiplayer
- Syntax Sugar
- Optimization Guide
- Benchmarks
- count
- select
- pushBack
- append
- deleteAt
- deleteRange
- resize
- reverse
- sort
- find
- in
- forEach
- freeze
- thaw
- isFrozen
- currentScheduler
- clientOwner
- allSchedulers
- schedulerName
- schedulerExists
- schedulerBudget
- setSchedulerBudget
- fiberCount
- readyFiberCount
- waitingFiberCount
- schedulerLoad
- sendTo