# Io Reference

Core / Core / Exception

The Exception proto is used for raising exceptions and instances are used to hold rexception related info.

Raise

An exception can be raised by calling raise() on an exception proto. Exception raise("generic foo exception")

Try and Catch

To catch an exception, the try() method of the Object proto is used. try() will catch any exceptions that occur within it and return the caught exception or nil if no exception is caught.

    e := try(<doMessage>)

To catch a particular exception, the Exception catch() method can be used. Example:

    e := try(
        // ...
    )

    e catch(Exception,
        writeln(e coroutine backtraceString)
    )

The first argument to catch indicates which types of exceptions will be caught. catch() returns the exception if it doesn't match and nil if it does.

Pass

To re-raise an exception caught by try(), use the pass method. This is useful to pass the exception up to the next outer exception handler, usually after all catches failed to match the type of the current exception:

    e := try(
        // ...
    )

    e catch(Error,
        // ...
    ) catch(Exception,
        // ...
    ) pass

Custom Exceptions

Custom exception types can be implemented by simply cloning an existing Exception type:

    MyErrorType := Error clone

catch(exceptionProto)

Catch an exception with the specified exception prototype.
caughtMessage

Returns the message object associated with the exception.
error

Returns the coroutine that the exception occurred in.
nestedException

Returns the nestedException if there is one.
originalCall

Returns the call object associated with the exception.
pass

Pass the exception up the stack.
raise(error, optionalNestedException)

Raise an exception with the specified error message.
showStack

Print the exception and related stack.