Version: v0.7.1 - Beta.  We welcome contributors & feedback.

Error Handling

Try/Catch 

When something goes wrong, it’s usually best to let the program die. A dead script can’t do any more damage, and is less likely to become a security vulnerability.

However, sometimes you want to provide a fallback for specific errors.

An Exception is an error that can be trapped with try/catch. The catch block will only run if there is an Exception in the try block.

$content = ''
try {
    // Will throw an Exception if it can't read the file.
    $content = File.read('important.txt')
}
catch $e {
    // Error. Try to restore it and continue.
    $content = getDefaultContent()
    File.write('important.txt', $content)
}

finally

A finally block can be used to define code that will run regardless of whether or not an Exception occurred.

This is optional, and is usually intended for cleanup logic that is invoked before the program ends.

try {
    // Initial code
}
catch $e {
    // Run if an Exception happens
    print($e.message())
}
finally {
    // Always run this after the above blocks complete
}

Die 

You can trigger an Exception manually with die.

if !fileExists {
    die('Important file not found.')
}

To halt the program without triggering an Exception, use System.exit().

Result Objects 

As an alternative to triggering an error or returning an error code, you can return a Result object.