- Introduction
- Editor
- Get started
- Runtime / VM
Language Guide
Design-Notes
Exceptions
Exceptions are used to handle runtime errors gracefully without crashing the virtual machine.
Throwing Exceptions
Use the throw keyword to throw an exception. The thrown object must be an instance of the Exception class or a class that extends it.
func test() {
throw Exception("Something went wrong")
}Try / Catch
Use try and catch to handle exceptions and prevent your program from terminating.
try {
// Code that might fail
}
catch (e) {
// Code to handle the error
}Example of catching an error:
try {
println("Start")
throw Exception("A fatal error occurred")
}
catch (e) {
println("Caught: ${e.message}")
}Important: Catch Type Constraint
The exception variable is always treated as the base Exception type. Writing explicit types like catch (e: CustomException) is forbidden by the compiler.
Handling Custom Exceptions
Since you can only catch the base Exception, you should use the is operator to check the specific error type at runtime.
class SkillIssueException extends Exception {
constructor(message: String) {
super(message)
}
}
try {
throw SkillIssueException("Developer forgot to drink coffee")
}
catch (e) {
if (e is SkillIssueException) {
println("Skill Issue detected: ${e.message}")
} else {
println("Unknown error: ${e.message}")
// Rethrow if we don't know how to handle it
throw e
}
}Rethrowing
If you catch an exception but realize you cannot handle it in the current scope, you can simply throw e again to pass it up the call stack.
func riskyOperation() {
try {
throw Exception("Database connection failed")
} catch (e) {
println("Logging error internally...")
throw e // Rethrow to the caller
}
}
riskyOperation()Location-Aware Exceptions
Since Autolang focuses on high performance and zero-overhead runtime, it does not generate heavy stack traces automatically. Instead, you can combine Exceptions with Magic Constants to track exactly where an error occurred.
// A custom exception that requires location tracking
class LoggedException extends Exception {
constructor(message: String, file: String, line: Int) {
// Format the error message with the exact location
super("[${file}:${line}] ${message}")
}
}
func parseData() {
try {
// Pass __FILE__ and __LINE__ when throwing the error
throw LoggedException("Failed to parse JSON", __FILE__, __LINE__)
} catch (e) {
println("Critical Error: ${e.message}")
}
}
parseData()
// Output: Critical Error: [main.al:12] Failed to parse JSON