Skip to main content

Exceptions & Error Handling Reference

Kyna features a robust, structured exception model using try, catch, finally, and throw. Both runtime errors and user-thrown exceptions flow through this unified mechanism.


The Error Object Model​

Every exception caught in Kyna exposes standard diagnostic fields:

  • code: A machine-readable error code string (e.g., KVM2301, KNET2001, K5100).
  • message: A human-readable description of the failure.
  • cause: The underlying exception or value, if chained.

try / catch Blocks​

To protect against errors, wrap risky operations in a try block:

try {
const response = fetch("https://api.kyna.dev/v1/data", { timeout: 3000 });
const payload = jsonParse(response);
print("Received payload:", payload);
} catch (err) {
print("Operation failed!");
print("Code:", err.code);
print("Message:", err.message);
}

The finally Clause​

A finally block always executes regardless of whether an exception occurred, was caught, or returned early:

fn processTransaction(): bool {
var lockAcquired = true;
print("Acquiring resource lock...");

try {
# Risky transactional work
throw new Error("DB_TIMEOUT", "Database failed to respond within deadline");
return true;
} catch (err) {
print("Caught transaction error:", err.message);
return false;
} finally {
lockAcquired = false;
print("Resource lock released.");
}
}

processTransaction();

Throwing Custom Errors​

Use throw with an Error instance or custom message:

fn validateAge(age: int) {
if (age < 0) {
throw new Error("VAL1001", "Age cannot be negative: " + age);
}
if (age > 150) {
throw new Error("VAL1002", "Age exceeds plausible threshold: " + age);
}
}

Common Compiler & Runtime Error Codes​

Code PrefixSubsystemExamples
K1000–K1999Compiler & Static Type CheckerK1001 Unbound variable, K1002 Const reassignment
KVM2000–KVM2999Virtual Machine ExecutionKVM2301 Type cast failure, KVM2302 Division by zero
KNET1000–KNET2999Networking & HTTPKNET2001 Connection timeout / DNS failure
K5100JSON / SerializationK5100 Malformed JSON parsing error