Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Use the current g_file_get_contents() as example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Clasen committed Jun 4, 2003
1 parent 284be13 commit a3720c8
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions docs/reference/glib/tmpl/error_reporting.sgml
Expand Up @@ -39,14 +39,17 @@ This is why most functions in GLib and GTK+ do not use the #GError facility.
Functions that can fail take a return location for a #GError as their last argument.
For example:
<informalexample><programlisting>
gchar* g_file_get_contents (const gchar *filename, GError **error);
gboolean g_file_get_contents (const gchar *filename,
gchar **contents,
gsize *length,
GError **error);
</programlisting></informalexample>
If you pass a non-%NULL value for the <literal>error</literal> argument, it should
point to a location where an error can be placed. For example:
<informalexample><programlisting>
gchar *contents;
GError *err = NULL;
contents = g_file_get_contents ("foo.txt", NULL, &amp;err);
g_file_get_contents ("foo.txt", &amp;contents, NULL, &amp;err);
g_assert ((contents == NULL &amp;&amp; err != NULL) || (contents != NULL &amp;&amp; err == NULL));
if (err != NULL)
{
Expand All @@ -63,18 +66,16 @@ else
</programlisting></informalexample>
Note that <literal>err != NULL</literal> in this example is a
<emphasis>reliable</emphasis> indicator of whether
g_file_get_contents() failed. Also, g_file_get_contents() uses the
convention that a %NULL return value means an error occurred (but not
all functions use this convention).
g_file_get_contents() failed. Additionally, g_file_get_contents() returns
a boolean which indicates whether it was successful.
</para>

<para>
Because g_file_get_contents() returns %NULL on failure, if you are only
Because g_file_get_contents() returns %FALSE on failure, if you are only
interested in whether it failed and don't need to display an error message, you
can pass %NULL for the <literal>error</literal> argument:
<informalexample><programlisting>
contents = g_file_get_contents ("foo.txt", NULL); /* ignore errors */
if (contents != NULL)
if (g_file_get_contents ("foo.txt", &contents, NULL, NULL)) /* ignore errors */
/* no error occurred */ ;
else
/* error */ ;
Expand Down

0 comments on commit a3720c8

Please sign in to comment.