~ubuntu-branches/debian/squeeze/glib2.0/squeeze

« back to all changes in this revision

Viewing changes to docs/reference/glib/html/glib-Error-Reporting.html

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2009-06-27 09:56:08 UTC
  • mfrom: (1.4.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090627095608-pi9244lmozmr2cm6
Tags: 2.20.4-1
New upstream bugfix release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
<head>
4
4
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
5
<title>Error Reporting</title>
6
 
<meta name="generator" content="DocBook XSL Stylesheets V1.74.3">
 
6
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
7
7
<link rel="home" href="index.html" title="GLib Reference Manual">
8
8
<link rel="up" href="glib-core.html" title="GLib Core Application Support">
9
9
<link rel="prev" href="glib-IO-Channels.html" title="IO Channels">
44
44
                <a href="#glib-Error-Reporting.description" class="shortcut">Description</a>
45
45
</td></tr>
46
46
</table>
47
 
<div class="refentry" lang="en">
 
47
<div class="refentry" title="Error Reporting">
48
48
<a name="glib-Error-Reporting"></a><div class="titlepage"></div>
49
49
<div class="refnamediv"><table width="100%"><tr>
50
50
<td valign="top">
53
53
</td>
54
54
<td valign="top" align="right"></td>
55
55
</tr></table></div>
56
 
<div class="refsynopsisdiv">
 
56
<div class="refsynopsisdiv" title="Synopsis">
57
57
<a name="glib-Error-Reporting.synopsis"></a><h2>Synopsis</h2>
58
58
<pre class="synopsis">
59
59
 
93
93
                                                         ...);
94
94
</pre>
95
95
</div>
96
 
<div class="refsect1" lang="en">
 
96
<div class="refsect1" title="Description">
97
97
<a name="glib-Error-Reporting.description"></a><h2>Description</h2>
98
98
<p>
99
99
GLib provides a standard method of reporting errors from a called function to
318
318
<p>
319
319
Error domains and codes are conventionally named as follows:
320
320
</p>
321
 
<div class="itemizedlist"><ul type="disc">
322
 
<li>
 
321
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
 
322
<li class="listitem">
323
323
<p>
324
324
The error domain is called
325
325
<code class="literal">&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR</code>, for example
336
336
<p>
337
337
</p>
338
338
</li>
339
 
<li><p>
 
339
<li class="listitem"><p>
340
340
The error codes are in an enumeration called
341
341
<code class="literal">&lt;Namespace&gt;&lt;Module&gt;Error</code>; for example,
342
342
<a class="link" href="glib-Threads.html#GThreadError" title="enum GThreadError"><span class="type">GThreadError</span></a> or <a class="link" href="glib-Spawning-Processes.html#GSpawnError" title="enum GSpawnError"><span class="type">GSpawnError</span></a>.
343
343
</p></li>
344
 
<li><p>
 
344
<li class="listitem"><p>
345
345
Members of the error code enumeration are called <code class="literal">&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_&lt;CODE&gt;</code>, for example <a class="link" href="glib-Spawning-Processes.html#G-SPAWN-ERROR-FORK--CAPS"><code class="literal">G_SPAWN_ERROR_FORK</code></a> or <a class="link" href="glib-Threads.html#G-THREAD-ERROR-AGAIN--CAPS"><code class="literal">G_THREAD_ERROR_AGAIN</code></a>.
346
346
</p></li>
347
 
<li><p>
 
347
<li class="listitem"><p>
348
348
If there's a "generic" or "unknown" error code for unrecoverable errors it
349
349
doesn't make sense to distinguish with specific codes, it should be called
350
350
<code class="literal">&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_FAILED</code>, for
356
356
<p>
357
357
Summary of rules for use of <span class="type">""</span>
358
358
      </p>
359
 
<div class="itemizedlist"><ul type="disc">
360
 
<li><p>
 
359
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
 
360
<li class="listitem"><p>
361
361
           Do not report programming errors via <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>.
362
362
          </p></li>
363
 
<li><p>
 
363
<li class="listitem"><p>
364
364
          The last argument of a function that returns an error should be a
365
365
          location where a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> can be placed (i.e. "<a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>** error").  If
366
366
          <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> is used with varargs, the <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>** should be the last
367
367
          argument before the "...".
368
368
        </p></li>
369
 
<li><p>
 
369
<li class="listitem"><p>
370
370
          The caller may pass <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> for the <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>** if they are not interested
371
371
          in details of the exact error that occurred.
372
372
        </p></li>
373
 
<li><p>
 
373
<li class="listitem"><p>
374
374
           If <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> is passed for the <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>** argument, then errors should
375
375
           not be returned to the caller, but your function should still
376
376
           abort and return if an error occurs. That is, control flow should
377
377
           not be affected by whether the caller wants to get a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>.
378
378
          </p></li>
379
 
<li><p>
 
379
<li class="listitem"><p>
380
380
          If a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> is reported, then your function by definition
381
381
          <span class="emphasis"><em>had a fatal failure and did not complete whatever it was supposed
382
382
            to do</em></span>. If the failure was not fatal, then you handled it
383
383
          and you should not report it. If it was fatal, then you must report it
384
384
          and discontinue whatever you were doing immediately.
385
385
        </p></li>
386
 
<li><p>
 
386
<li class="listitem"><p>
387
387
          A <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>* must be initialized to <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> before passing its address to
388
388
          a function that can report errors.
389
389
          </p></li>
390
 
<li><p>
 
390
<li class="listitem"><p>
391
391
          "Piling up" errors is always a bug. That is, if you assign a new
392
392
          <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> to a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>* that is non-<a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a>, thus overwriting the previous
393
393
          error, it indicates that you should have aborted the operation instead
395
395
          the previous error with <a class="link" href="glib-Error-Reporting.html#g-clear-error" title="g_clear_error ()"><code class="function">g_clear_error()</code></a>. <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()"><code class="function">g_set_error()</code></a> will complain
396
396
          if you pile up errors.
397
397
          </p></li>
398
 
<li><p>
 
398
<li class="listitem"><p>
399
399
          By convention, if you return a boolean value indicating success
400
400
          then <a class="link" href="glib-Standard-Macros.html#TRUE--CAPS" title="TRUE"><code class="literal">TRUE</code></a> means success and <a class="link" href="glib-Standard-Macros.html#FALSE--CAPS" title="FALSE"><code class="literal">FALSE</code></a> means failure. If <a class="link" href="glib-Standard-Macros.html#FALSE--CAPS" title="FALSE"><code class="literal">FALSE</code></a> is returned,
401
401
          the error <span class="emphasis"><em>must</em></span> be set to a non-<a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> value.
402
402
        </p></li>
403
 
<li><p>
 
403
<li class="listitem"><p>
404
404
          A <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> return value is also frequently used to mean that an error
405
405
          occurred.  You should make clear in your documentation whether <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> is
406
406
          a valid return value in non-error cases; if <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> is a valid value,
407
407
          then users must check whether an error was returned to see if the
408
408
          function succeeded.
409
409
          </p></li>
410
 
<li><p>
 
410
<li class="listitem"><p>
411
411
          When implementing a function that can report errors, you may want to
412
412
          add a check at the top of your function that the error return location
413
413
          is either <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> or contains a <a class="link" href="glib-Standard-Macros.html#NULL--CAPS" title="NULL"><code class="literal">NULL</code></a> error
418
418
<p>
419
419
</p>
420
420
</div>
421
 
<div class="refsect1" lang="en">
 
421
<div class="refsect1" title="Details">
422
422
<a name="glib-Error-Reporting.details"></a><h2>Details</h2>
423
 
<div class="refsect2" lang="en">
 
423
<div class="refsect2" title="GError">
424
424
<a name="GError"></a><h3>GError</h3>
425
425
<pre class="programlisting">typedef struct {
426
426
  GQuark       domain;
454
454
</table></div>
455
455
</div>
456
456
<hr>
457
 
<div class="refsect2" lang="en">
 
457
<div class="refsect2" title="g_error_new ()">
458
458
<a name="g-error-new"></a><h3>g_error_new ()</h3>
459
459
<pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a>*             g_error_new                         (<a class="link" href="glib-Quarks.html#GQuark" title="GQuark">GQuark</a> domain,
460
460
                                                         <a class="link" href="glib-Basic-Types.html#gint" title="gint">gint</a> code,
497
497
</table></div>
498
498
</div>
499
499
<hr>
500
 
<div class="refsect2" lang="en">
 
500
<div class="refsect2" title="g_error_new_literal ()">
501
501
<a name="g-error-new-literal"></a><h3>g_error_new_literal ()</h3>
502
502
<pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a>*             g_error_new_literal                 (<a class="link" href="glib-Quarks.html#GQuark" title="GQuark">GQuark</a> domain,
503
503
                                                         <a class="link" href="glib-Basic-Types.html#gint" title="gint">gint</a> code,
536
536
</table></div>
537
537
</div>
538
538
<hr>
539
 
<div class="refsect2" lang="en">
 
539
<div class="refsect2" title="g_error_free ()">
540
540
<a name="g-error-free"></a><h3>g_error_free ()</h3>
541
541
<pre class="programlisting">void                g_error_free                        (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> *error);</pre>
542
542
<p>
553
553
</table></div>
554
554
</div>
555
555
<hr>
556
 
<div class="refsect2" lang="en">
 
556
<div class="refsect2" title="g_error_copy ()">
557
557
<a name="g-error-copy"></a><h3>g_error_copy ()</h3>
558
558
<pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a>*             g_error_copy                        (const <a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> *error);</pre>
559
559
<p>
577
577
</table></div>
578
578
</div>
579
579
<hr>
580
 
<div class="refsect2" lang="en">
 
580
<div class="refsect2" title="g_error_matches ()">
581
581
<a name="g-error-matches"></a><h3>g_error_matches ()</h3>
582
582
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean">gboolean</a>            g_error_matches                     (const <a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> *error,
583
583
                                                         <a class="link" href="glib-Quarks.html#GQuark" title="GQuark">GQuark</a> domain,
614
614
</table></div>
615
615
</div>
616
616
<hr>
617
 
<div class="refsect2" lang="en">
 
617
<div class="refsect2" title="g_set_error ()">
618
618
<a name="g-set-error"></a><h3>g_set_error ()</h3>
619
619
<pre class="programlisting">void                g_set_error                         (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> **err,
620
620
                                                         <a class="link" href="glib-Quarks.html#GQuark" title="GQuark">GQuark</a> domain,
658
658
</table></div>
659
659
</div>
660
660
<hr>
661
 
<div class="refsect2" lang="en">
 
661
<div class="refsect2" title="g_set_error_literal ()">
662
662
<a name="g-set-error-literal"></a><h3>g_set_error_literal ()</h3>
663
663
<pre class="programlisting">void                g_set_error_literal                 (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> **err,
664
664
                                                         <a class="link" href="glib-Quarks.html#GQuark" title="GQuark">GQuark</a> domain,
700
700
<p class="since">Since 2.18</p>
701
701
</div>
702
702
<hr>
703
 
<div class="refsect2" lang="en">
 
703
<div class="refsect2" title="g_propagate_error ()">
704
704
<a name="g-propagate-error"></a><h3>g_propagate_error ()</h3>
705
705
<pre class="programlisting">void                g_propagate_error                   (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> **dest,
706
706
                                                         <a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> *src);</pre>
726
726
</table></div>
727
727
</div>
728
728
<hr>
729
 
<div class="refsect2" lang="en">
 
729
<div class="refsect2" title="g_clear_error ()">
730
730
<a name="g-clear-error"></a><h3>g_clear_error ()</h3>
731
731
<pre class="programlisting">void                g_clear_error                       (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> **err);</pre>
732
732
<p>
744
744
</table></div>
745
745
</div>
746
746
<hr>
747
 
<div class="refsect2" lang="en">
 
747
<div class="refsect2" title="g_prefix_error ()">
748
748
<a name="g-prefix-error"></a><h3>g_prefix_error ()</h3>
749
749
<pre class="programlisting">void                g_prefix_error                      (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> **err,
750
750
                                                         const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar">gchar</a> *format,
786
786
<p class="since">Since 2.16</p>
787
787
</div>
788
788
<hr>
789
 
<div class="refsect2" lang="en">
 
789
<div class="refsect2" title="g_propagate_prefixed_error ()">
790
790
<a name="g-propagate-prefixed-error"></a><h3>g_propagate_prefixed_error ()</h3>
791
791
<pre class="programlisting">void                g_propagate_prefixed_error          (<a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> **dest,
792
792
                                                         <a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a> *src,