Software: Apache/2.0.54 (Fedora). PHP/5.0.4 uname -a: Linux mina-info.me 2.6.17-1.2142_FC4smp #1 SMP Tue Jul 11 22:57:02 EDT 2006 i686 uid=48(apache) gid=48(apache) groups=48(apache) Safe-mode: OFF (not secure) /usr/share/gtk-doc/html/glib/ drwxr-xr-x |
Viewing file: Select action/file-type:
Synopsis#include <glib.h> GError; GError* g_error_new (GQuark domain, gint code, const gchar *format, ...); GError* g_error_new_literal (GQuark domain, gint code, const gchar *message); void g_error_free (GError *error); GError* g_error_copy (const GError *error); gboolean g_error_matches (const GError *error, GQuark domain, gint code); void g_set_error (GError **err, GQuark domain, gint code, const gchar *format, ...); void g_propagate_error (GError **dest, GError *src); void g_clear_error (GError **err); DescriptionGLib provides a standard method of reporting errors from a called function to the calling code. (This is the same problem solved by exceptions in other languages.) It's important to understand that this method is both a data type (the GError object) and a set of rules. If you use GError incorrectly, then your code will not properly interoperate with other code that uses GError, and users of your API will probably get confused.
First and foremost: GError should only be used to report
recoverable runtime errors, never to report programming errors. If
the programmer has screwed up, then you should use
Examples of recoverable runtime errors are "file not found" or "failed to parse
input." Examples of programming errors are "NULL passed to Functions that can fail take a return location for a GError as their last argument. For example: gboolean g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error);
If you pass a non- gchar *contents; GError *err = NULL; g_file_get_contents ("foo.txt", &contents, NULL, &err); g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL)); if (err != NULL) { /* Report error to user, and free error */ g_assert (contents == NULL); fprintf (stderr, "Unable to read file: %s\n", err->message); g_error_free (err); } else { /* Use file contents */ g_assert (contents != NULL); }
Note that
Because if (g_file_get_contents ("foo.txt", &contents, NULL, NULL)) /* ignore errors */ /* no error occurred */ ; else /* error */ ;
The GError object contains three fields:
When implementing a function that can report errors, the basic tool is
gint foo_open_file (GError **error) { gint fd; fd = open ("file.txt", O_RDONLY); if (fd < 0) { g_set_error (error, FOO_ERROR, /* error domain */ FOO_ERROR_BLAH, /* error code */ "Failed to open file: %s", /* error message format string */ g_strerror (errno)); return -1; } else return fd; }
Things are somewhat more complicated if you yourself call another function that
can report a GError. If the sub-function indicates fatal errors in some way
other than reporting a GError, such as by returning gboolean my_function_that_can_fail (GError **err) { g_return_val_if_fail (err == NULL || *err == NULL, FALSE); if (!sub_function_that_can_fail (err)) { /* assert that error was set by the sub-function */ g_assert (err == NULL || *err != NULL); return FALSE; } /* otherwise continue, no error occurred */ g_assert (err == NULL || *err == NULL); }
If the sub-function does not indicate errors other than by reporting a GError,
you need to create a temporary GError since the passed-in one may be gboolean my_function_that_can_fail (GError **err) { GError *tmp_error; g_return_val_if_fail (err == NULL || *err == NULL, FALSE); tmp_error = NULL; sub_function_that_can_fail (&tmp_error); if (tmp_error != NULL) { /* store tmp_error in err, if err != NULL, * otherwise call g_error_free() on tmp_error */ g_propagate_error (err, tmp_error); return FALSE; } /* otherwise continue, no error occurred */ }
Error pileups are always a bug. For example, this code is incorrect: gboolean my_function_that_can_fail (GError **err) { GError *tmp_error; g_return_val_if_fail (err == NULL || *err == NULL, FALSE); tmp_error = NULL; sub_function_that_can_fail (&tmp_error); other_function_that_can_fail (&tmp_error); if (tmp_error != NULL) { g_propagate_error (err, tmp_error); return FALSE; } }
gboolean my_function_that_can_fail (GError **err) { GError *tmp_error; g_return_val_if_fail (err == NULL || *err == NULL, FALSE); sub_function_that_can_fail (NULL); /* ignore errors */ tmp_error = NULL; other_function_that_can_fail (&tmp_error); if (tmp_error != NULL) { g_propagate_error (err, tmp_error); return FALSE; } }
Note that passing Error domains and codes are conventionally named as follows:
Summary of rules for use of GError:
DetailsGErrortypedef struct { GQuark domain; gint code; gchar *message; } GError; The GError structure contains information about an error that has occurred.
g_error_new ()GError* g_error_new (GQuark domain, gint code, const gchar *format, ...);
Creates a new GError with the given
g_error_new_literal ()GError* g_error_new_literal (GQuark domain, gint code, const gchar *message);
Creates a new GError; unlike
g_error_free ()void g_error_free (GError *error); Frees a GError and associated resources.
g_error_matches ()gboolean g_error_matches (const GError *error, GQuark domain, gint code);
Returns
g_set_error ()void g_set_error (GError **err, GQuark domain, gint code, const gchar *format, ...);
Does nothing if
g_propagate_error ()void g_propagate_error (GError **dest, GError *src);
If
g_clear_error ()void g_clear_error (GError **err);
If
|
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #16 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.0042 ]-- |