# Error Handling

GLib has its own method of handling errors using `GLib.Error`. These are raised as Python exceptions, but with a few small differences.

It’s common in Python for exception subclasses to be used (e.g., `ValueError` versus `IOError`) to distinguish different types of errors. Libraries often define their own `Exception` subclasses, and library users will handle these cases explicitly.

In GLib-using libraries, errors are all `GLib.Error` instances, with no subclassing for different error types. Instead, every `GLib.Error` instance has attributes that distinguish types of error:

- `GLib.Error.domain` is the error domain, usually a string that you can convert to a `GLib` quark with `GLib.quark_from_string()`

- `GLib.Error.code` identifies a specific error within the domain

- `GLib.Error.message` is a human-readable description of the error

Error domains are defined per-module, and you can get an error domain from `*_error_quark` functions on the relevant module. For example, IO errors from `Gio` are in the domain returned by `Gio.io_error_quark()`, and possible error code values are enumerated in `Gio.IOErrorEnum`.

Once you’ve caught a `GLib.Error`, you can call `GLib.Error.matches()` to see whether it matches the specific error you want to handle.

## Examples

Catching a specific error:

    >>> from gi.repository import GLib, Gio
    >>> f = Gio.File.new_for_path('missing-path')
    >>> try:
    ...     f.read()
    ... except GLib.Error as err:
    ...     if err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND):
    ...         print('File not found')
    ...     else:
    ...         raise
    File not found