LEVIATHAN v962456e · 962456eee1

Standard Library

class Exception

The base class of the standard exceptions, holding a message.

since 0.1.0-alpha.1linuxwindowswasm

bases
IException

Overview

Derive from it to make exceptions of your own: call Exception::Exception(msg) from your constructor to set the message. Throw one with throw and handle it with catch; the exception is caught by the nearest catch whose type it is an instance of.

Examples

A custom exception

class NotFoundException : Exception {
    new NotFoundException(string what) {
        Exception::Exception("not found: ${what}");
    }
}
try {
    throw NotFoundException("user 7");
} catch (Exception e) {
    console.writeln(e.message);
}
not found: user 7

Constructors

new

new(string msg)

Creates an exception with a message.

Parameters

msg
The text describing what went wrong.

Examples

Exception e = Exception("plain");
console.writeln(e.message);
plain

Fields

message

string message

The text that describes what went wrong.

Methods

toString

toString() -> string

Returns the message.

Returns

The same text as message.

Examples

Exception e = Exception("disk full");
console.writeln(e.toString());
disk full

See also

  • IException — The interface that every thrown value must implement.
  • RuntimeException — The error the standard library throws when an operation fails at run time.
  • LogicException — An error that signals a mistake in the program's own logic, such as using an object in the wrong state.