IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

IOError

struct IOError

Represents a failed I/O operation.

Examples:

from std.io import IOError

def read_file(path: String) raises IOError -> String:
raise IOError(.not_found, "file not found", operation="open")

try:
_ = read_file("foo.txt")
except e:
match e.kind():
case .not_found:
print("missing:", e) # missing: open: file not found
case .permission_denied:
print("access denied:", e)
case _:
print("failed:", e)

Implemented traits​

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable, Writable

Methods​

__init__​

def __init__(out self, kind: IOErrorKind, message: StaticString, *, operation: StaticString)

Constructs an error that didn't come from the operating system.

Args:

last_os_error​

static def last_os_error(operation: StaticString) -> Self

Returns an error for the calling thread's current errno.

Args:

  • ​operation (StaticString): The name of the operation that failed.

Returns:

Self: The error for the current errno.

kind​

def kind(self) -> IOErrorKind

Returns the kind of error.

Returns:

IOErrorKind: The kind passed to the constructor, or for an operating system error, the kind that its platform error maps to.

write_to​

def write_to(self, mut writer: T)

Writes the error to a writer.

Args:

  • ​writer (T): The writer to write to.

write_repr_to​

def write_repr_to(self, mut writer: T)

Writes the error's debug representation to a writer.

Args:

  • ​writer (T): The writer to write to.