Exceptions
import "std/core" (included in import "std") · namespace std.core
The standard exception classes. Everything the standard library throws, and everything the language itself throws when the standard library is loaded, is one of these. Extend Exception for your own errors.
| Class | Thrown when |
|---|---|
Exception | the base class: catch it to catch everything |
ValueError | an argument or input is unsuitable; also n as Enum with a bad number |
TypeError | a value has the wrong type: a failed obj as Class or obj as Interface |
IndexError | an index is out of range: arrays, strings, collections, Bytes |
ArithmeticError | integer division or % by zero |
StateError | the object is not in a state that allows the call: popping an empty stack, x! on null, unwrap() on an Err |
NotFound | a lookup found nothing (Map.get, SortedMap.get) |
IOError | a file, process or network operation failed (NetError is a subclass) |
TimeoutError | an operation took longer than allowed |
RuntimeError | anything else |
Every exception has message() (the text it was made with), kind() (its kind, like "ValueError") and describe() ("Kind: message", also what print shows and what an uncaught exception prints).
import "std"
class QuotaExceeded extends Exception {
public used: int
constructor(used: int) {
super("quota exceeded: ${used} MB")
self.used = used
}
fn kind() -> string { return "QuotaExceeded" }
}
fn upload(mb: int) {
if (mb < 0) { throw new ValueError("negative size") }
if (mb > 100) { throw new QuotaExceeded(mb) }
}
fn main() {
for (size in [10, -1, 500]) {
try {
upload(size)
print("uploaded ${size} MB")
} catch (e: QuotaExceeded) {
print("${e.describe()} (used ${e.used})")
} catch (e: Exception) {
print("failed: ${e.describe()}")
}
}
}uploaded 10 MB
failed: ValueError: negative size
QuotaExceeded: quota exceeded: 500 MB (used 500)See Errors and exceptions for throw, try, catch and finally.
API
class Exception
Constructors
new Exception(m: string)
Methods
fn message() -> string— The message the exception was created with.fn kind() -> string— The exception's kind ("ValueError", ...): its class name for the standard ones.fn describe() -> string— "Kind: message" — what an uncaught exception prints.fn toString() -> string— The same as describe().
class ValueError extends Exception
A value is unsuitable (bad argument, unparsable input).
Constructors
new ValueError(m: string)
Methods
fn kind() -> string— "ValueError".
class TypeError extends Exception
A value has the wrong type (e.g. an any holding something unexpected).
Constructors
new TypeError(m: string)
Methods
fn kind() -> string— "TypeError".
class IndexError extends Exception
An index or position is out of range.
Constructors
new IndexError(m: string)
Methods
fn kind() -> string— "IndexError".
class NotFound extends Exception
A key, file, or record does not exist.
Constructors
new NotFound(m: string)
Methods
fn kind() -> string— "NotFound".
class IOError extends Exception
Reading or writing a file, pipe, or device failed.
Constructors
new IOError(m: string)
Methods
fn kind() -> string— "IOError".
class StateError extends Exception
An operation is not valid in the object's current state.
Constructors
new StateError(m: string)
Methods
fn kind() -> string— "StateError".
class TimeoutError extends Exception
Something took longer than allowed.
Constructors
new TimeoutError(m: string)
Methods
fn kind() -> string— "TimeoutError".
class ArithmeticError extends Exception
Integer division (or %) by zero.
Constructors
new ArithmeticError(m: string)
Methods
fn kind() -> string— "ArithmeticError".
class RuntimeError extends Exception
Any other failure while the program runs.
Constructors
new RuntimeError(m: string)
Methods
fn kind() -> string— "RuntimeError".