Skip to main content

Exceptions

lib/exceptions/ defines the exception hierarchy.

Hierarchy

Exception
├── RuntimeException
│ ├── IllegalArgumentException
│ ├── IndexOutOfBoundsException
│ ├── NullPointerException
│ └── ClassNotFoundException
└── (your custom exception classes)

All exceptions extend Exception and carry a message accessible via getMessage().

Throwing

import * from "lib/exceptions/IllegalArgumentException.mt";

if (input == "") {
throw new IllegalArgumentException("input cannot be empty");
}

Catching

import * from "lib/exceptions/Exception.mt";

try {
risky();
} catch (Exception e) {
print("error: " + e.getMessage());
}

A single try can have multiple catch clauses for different exception types; the first compatible one runs.

Custom Exceptions

import * from "lib/exceptions/Exception.mt";

class ValidationException extends Exception {
public constructor(string message): super(message) { }
}

Declare what a method throws using @Throw:

@Throw(exceptions = [ValidationException])
public function validate(string input): bool { ... }

Network Exceptions

lib/net/exceptions/ provides network-specific subclasses:

  • NetworkException
  • ConnectionException
  • DnsException
  • HttpException
  • TimeoutException

See Network for how they're raised.

See Also