# Exceptions

The standard library provides the type `core.Exception`, which is the root class for all exceptions in Storm. Each exception contains a message, and optionally also a stack trace. It has the following members:

- `core.Str message()`

  Retrieve the message in the exception. This function simply calls `message(StrBuf)` to generate the message.

- `void message(core.StrBuf to)`

  Create the message by appending it to the string buffer.

- `core.StackTrace stackTrace`

  Collected stack trace, if any.

- `core.Exception saveTrace()`

  Convenience function to call to collect a stack trace. Intended to be called from the constructor of derived exceptions who wish to store a stack trace. This is not done by default, as it is fairly expensive, and not beneficial for all exceptions (e.g. syntax errors). Some categories of exceptions do, however, capture stack traces automatically. Returns `this` to allow chaining it after creation.

The `saveTrace` function returns the exception itself. This makes it convenient to add a stack trace to an exception temporarily by modifying code like:

    throw MyException();

into:

    throw MyException().saveTrace();

## Exception Categories

The following exceptions are provided in the standard library:

- `core.RuntimeError` extends `Exception`

  Depicts a runtime error from some part of the system. The following subclasses are provided:

  - `core.GcError` extends `RuntimeError`

    Thrown when the garbage collector detects an issue. Typically when available memory is low.

  - `core.NumericError` extends `RuntimeError`

    Thrown when a numeric operation failed.

    - `core.DivisionByZero` extends `NumericError`

      Thrown whenever division by zero is attempted.

  - `core.MemoryAccessError` extends `RuntimeError`

    Thrown when an invalid memory address was accessed.

  - `core.InternalError` extends `RuntimeError`

    Generic form of internal error.

  - `core.AbstractFnCalled` extends `RuntimeError`

    Thrown when an abstract function was called.

  - `core.AssertError` extends `RuntimeError`

    Thrown when an assertion fails.

- `core.NotSupported` extends `Exception`

  Thrown when an operation is not supported.

- `core.UsageError` extends `Exception`

  Thrown when an interface is used incorrectly.

- `core.StrError` extends `Exception`

  Errors from `core.Str`.

- `core.MapError` extends `Exception`

  Errors from `core.Map` and `core.RefMap`.

- `core.SetError` extends `Exception`

  Errors from `core.Set` and `core.RefSet`.

- `core.ArrayError` extends `Exception`

  Errors from `core.Array`.