Philip Withnall
philip.withnall@collabora.co.uk 2015

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

Als besondere Ausnahme gewähren die Inhaber des Urheberrechts die Erlaubnis, den Beispiel-Code in diesem Dokument ohne Einschränkungen unter den Bedingungen Ihrer Wahl zu kopieren, zu verändern und weiterzugeben.

Umgang mit und Melden von Laufzeitfehlern
Mario Blättermann
mario.blaettermann@gmail.com 2016, 2018
Christian Kirbach
christian.kirbach@gmail.com 2016, 2020
Stefan Melmuk
stefan.melmuk@gmail.com 2018
Tim Sabsch
tim@sabsch.com 2021

# GError

# Verwendung von GError

`GError` is the standard error reporting mechanism for GLib-using code, and can be thought of as a C implementation of an exception.

Any kind of runtime failure (anything which is not a programmer error) must be handled by including a `GError**` parameter in the function, and setting a useful and relevant GError describing the failure, before returning from the function. Programmer errors must not be handled using GError: use assertions, pre-conditions or post-conditions instead.

GError should be used in preference to a simple return code, as it can convey more information, and is also supported by all GLib tools. For example, introspecting an API will automatically detect all GError parameters so that they can be converted to exceptions in other languages.

Printing warnings to the console must not be done in library code: use a GError, and the calling code can propagate it further upwards, decide to handle it, or decide to print it to the console. Ideally, the only code which prints to the console will be top-level application code, and not library code.

Any function call which can take a `GError**`, should take such a parameter, and the returned GError should be handled appropriately. There are very few situations where ignoring a potential error by passing `NULL` to a `GError**` parameter is acceptable.

Die GLib-API-Dokumentation enthält ein vollständiges Tutorial zur Verwendung von GError.