Atom

Exceptions

Catch a problem and clean up

An exception stops the normal path through a block. Catch it where you can handle the problem. Use finally for work that must happen when the block exits.

Atom
try:
    throw 'missing input'
catch problem:
    print(problem)
finally:
    print('cleanup')
Output
missing input
cleanup

The catch prints the thrown value. The finally block runs after the handler.

Close a real resource

A file has an operating-system resource behind it. Close the handle in finally so it is released if the write raises.

Atom
use fs
path = fs.temporary_file()
handle = fs.open(path, 'w')
try:
    write(handle, 'Atom')
finally:
    fs.close(handle)

print(read(path))
fs.remove(path)
Output
Atom

Closing the handle releases it and flushes the write before the file is read by name.

Try and catch

Put operations that may raise in a 'try' block. A 'catch' receives the thrown value or a runtime error message.

Throw

Use 'throw value' to report a failure. The value may be a dictionary with details useful to the caller.

Finally and resource cleanup

Put deterministic cleanup in 'finally'. Use the resource's own close function when there is one.

Throw accepts any Atom value. A catch binds the thrown value or the message of a runtime exception. Try can have catch, finally, or both. Finally runs after a successful try, after a catchable error, and as control leaves through return, break or continue. Execution limits, interruption and other hard failures such as allocation failure can bypass script handlers. catch handles thrown values and runtime exceptions. Runtime diagnostics include source locations and a stack trace.

A catch receives the thrown Atom value as-is, including a dictionary or null. Runtime-generated exceptions carry text. Use a finally block for closing a file, socket, process, database or native library that was successfully opened. An error in a called function or a module's initialization can propagate to an enclosing handler. A failed import is removed from the initialization cache. Syntax or compilation failure while loading a module ends execution with the corresponding compiler status. Coroutine errors propagate from task.resume. A failed worker becomes a runtime error from task.wait using the worker diagnostic message and trace. FFI errors from a synchronous Atom callback are reported after the C call returns. An invalid native signature can crash or corrupt the process outside Atom's error mechanism.

Atom
use fs
filename = fs.temporary_file()
try:
    handle = fs.open(filename, 'w+')
    try:
        write(handle, 'café')
        fs.seek(handle, 0)
        assert(read(handle) == 'café')
    finally:
        fs.close(handle)
finally:
    fs.remove(filename)