PageRenderTime 74ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/glib-2.25.17/docs/reference/glib/html/glib-Error-Reporting.html

https://bitbucket.org/ArminW/3p-glib
HTML | 1026 lines | 1015 code | 11 blank | 0 comment | 0 complexity | 69f4f01d0b03c5fc7ff16887ee124d1c MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Error Reporting</title>
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
  7. <link rel="home" href="index.html" title="GLib Reference Manual">
  8. <link rel="up" href="glib-core.html" title="GLib Core Application Support">
  9. <link rel="prev" href="glib-IO-Channels.html" title="IO Channels">
  10. <link rel="next" href="glib-Warnings-and-Assertions.html" title="Message Output and Debugging Functions">
  11. <meta name="generator" content="GTK-Doc V1.15.1 (XML mode)">
  12. <link rel="stylesheet" href="style.css" type="text/css">
  13. </head>
  14. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  15. <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2">
  16. <tr valign="middle">
  17. <td><a accesskey="p" href="glib-IO-Channels.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
  18. <td><a accesskey="u" href="glib-core.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
  19. <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
  20. <th width="100%" align="center">GLib Reference Manual</th>
  21. <td><a accesskey="n" href="glib-Warnings-and-Assertions.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
  22. </tr>
  23. <tr><td colspan="5" class="shortcuts">
  24. <a href="#glib-Error-Reporting.synopsis" class="shortcut">Top</a>
  25.  | 
  26. <a href="#glib-Error-Reporting.description" class="shortcut">Description</a>
  27. </td></tr>
  28. </table>
  29. <div class="refentry">
  30. <a name="glib-Error-Reporting"></a><div class="titlepage"></div>
  31. <div class="refnamediv"><table width="100%"><tr>
  32. <td valign="top">
  33. <h2><span class="refentrytitle"><a name="glib-Error-Reporting.top_of_page"></a>Error Reporting</span></h2>
  34. <p>Error Reporting a system for reporting errors</p>
  35. </td>
  36. <td valign="top" align="right"></td>
  37. </tr></table></div>
  38. <div class="refsynopsisdiv">
  39. <a name="glib-Error-Reporting.synopsis"></a><h2>Synopsis</h2>
  40. <pre class="synopsis">
  41. #include &lt;glib.h&gt;
  42. <a class="link" href="glib-Error-Reporting.html#GError" title="GError">GError</a>;
  43. <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * <a class="link" href="glib-Error-Reporting.html#g-error-new" title="g_error_new ()">g_error_new</a> (<em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  44. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  45. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  46. <em class="parameter"><code>...</code></em>);
  47. <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * <a class="link" href="glib-Error-Reporting.html#g-error-new-literal" title="g_error_new_literal ()">g_error_new_literal</a> (<em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  48. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  49. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *message</code></em>);
  50. <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * <a class="link" href="glib-Error-Reporting.html#g-error-new-valist" title="g_error_new_valist ()">g_error_new_valist</a> (<em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  51. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  52. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  53. <em class="parameter"><code><span class="type">va_list</span> args</code></em>);
  54. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-error-free" title="g_error_free ()">g_error_free</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *error</code></em>);
  55. <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * <a class="link" href="glib-Error-Reporting.html#g-error-copy" title="g_error_copy ()">g_error_copy</a> (<em class="parameter"><code>const <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *error</code></em>);
  56. <a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a> <a class="link" href="glib-Error-Reporting.html#g-error-matches" title="g_error_matches ()">g_error_matches</a> (<em class="parameter"><code>const <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *error</code></em>,
  57. <em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  58. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>);
  59. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()">g_set_error</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>,
  60. <em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  61. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  62. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  63. <em class="parameter"><code>...</code></em>);
  64. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-set-error-literal" title="g_set_error_literal ()">g_set_error_literal</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>,
  65. <em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  66. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  67. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *message</code></em>);
  68. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-propagate-error" title="g_propagate_error ()">g_propagate_error</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **dest</code></em>,
  69. <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *src</code></em>);
  70. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-clear-error" title="g_clear_error ()">g_clear_error</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>);
  71. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-prefix-error" title="g_prefix_error ()">g_prefix_error</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>,
  72. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  73. <em class="parameter"><code>...</code></em>);
  74. <span class="returnvalue">void</span> <a class="link" href="glib-Error-Reporting.html#g-propagate-prefixed-error" title="g_propagate_prefixed_error ()">g_propagate_prefixed_error</a> (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **dest</code></em>,
  75. <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *src</code></em>,
  76. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  77. <em class="parameter"><code>...</code></em>);
  78. </pre>
  79. </div>
  80. <div class="refsect1">
  81. <a name="glib-Error-Reporting.description"></a><h2>Description</h2>
  82. <p>
  83. GLib provides a standard method of reporting errors from a called function to
  84. the calling code. (This is the same problem solved by exceptions in other
  85. languages.) It's important to understand that this method is both a
  86. <span class="emphasis"><em>data type</em></span> (the <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> object) and a <span class="emphasis"><em>set of
  87. rules.</em></span> If you use <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> incorrectly, then your code will not
  88. properly interoperate with other code that uses <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>, and users of your API
  89. will probably get confused.
  90. </p>
  91. <p>
  92. First and foremost: <span class="emphasis"><em><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> should only be used to report
  93. recoverable runtime errors, never to report programming errors.</em></span> If
  94. the programmer has screwed up, then you should use <a class="link" href="glib-Message-Logging.html#g-warning" title="g_warning ()"><code class="function">g_warning()</code></a>,
  95. <a class="link" href="glib-Warnings-and-Assertions.html#g-return-if-fail" title="g_return_if_fail()"><code class="function">g_return_if_fail()</code></a>, <a class="link" href="glib-Testing.html#g-assert" title="g_assert()"><code class="function">g_assert()</code></a>, <a class="link" href="glib-Message-Logging.html#g-error" title="g_error ()"><code class="function">g_error()</code></a>, or some similar facility.
  96. (Incidentally, remember that the <a class="link" href="glib-Message-Logging.html#g-error" title="g_error ()"><code class="function">g_error()</code></a> function should
  97. <span class="emphasis"><em>only</em></span> be used for programming errors, it should not be used
  98. to print any error reportable via <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>.)
  99. </p>
  100. <p>
  101. Examples of recoverable runtime errors are "file not found" or "failed to parse
  102. input." Examples of programming errors are "NULL passed to <code class="function">strcmp()</code>" or
  103. "attempted to free the same pointer twice." These two kinds of errors are
  104. fundamentally different: runtime errors should be handled or reported to the
  105. user, programming errors should be eliminated by fixing the bug in the program.
  106. This is why most functions in GLib and GTK+ do not use the <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> facility.
  107. </p>
  108. <p>
  109. Functions that can fail take a return location for a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> as their last argument.
  110. For example:
  111. </p>
  112. <div class="informalexample">
  113. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  114. <tbody>
  115. <tr>
  116. <td class="listing_lines" align="right"><pre>1
  117. 2
  118. 3
  119. 4</pre></td>
  120. <td class="listing_code"><pre class="programlisting"><span class="usertype">gboolean</span><span class="normal"> </span><span class="function"><a href="glib-File-Utilities.html#g-file-get-contents">g_file_get_contents</a></span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">const</span><span class="normal"> </span><span class="usertype">gchar</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">filename</span><span class="symbol">,</span>
  121. <span class="normal"> </span><span class="usertype">gchar</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">contents</span><span class="symbol">,</span>
  122. <span class="normal"> </span><span class="usertype">gsize</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">length</span><span class="symbol">,</span>
  123. <span class="normal"> </span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">error</span><span class="symbol">);</span></pre></td>
  124. </tr>
  125. </tbody>
  126. </table>
  127. </div>
  128. <p>
  129. If you pass a non-<a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> value for the <code class="literal">error</code> argument, it should
  130. point to a location where an error can be placed. For example:
  131. </p>
  132. <div class="informalexample">
  133. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  134. <tbody>
  135. <tr>
  136. <td class="listing_lines" align="right"><pre>1
  137. 2
  138. 3
  139. 4
  140. 5
  141. 6
  142. 7
  143. 8
  144. 9
  145. 10
  146. 11
  147. 12
  148. 13
  149. 14
  150. 15
  151. 16</pre></td>
  152. <td class="listing_code"><pre class="programlisting"><span class="usertype">gchar</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">contents</span><span class="symbol">;</span>
  153. <span class="usertype">GError</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">;</span>
  154. <span class="function"><a href="glib-File-Utilities.html#g-file-get-contents">g_file_get_contents</a></span><span class="normal"> </span><span class="symbol">(</span><span class="string">"foo.txt"</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal">contents</span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal">err</span><span class="symbol">);</span>
  155. <span class="function"><a href="glib-Testing.html#g-assert">g_assert</a></span><span class="normal"> </span><span class="symbol">((</span><span class="normal">contents </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">&amp;&amp;</span><span class="normal"> err </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">contents </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">&amp;&amp;</span><span class="normal"> err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">));</span>
  156. <span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">)</span>
  157. <span class="normal"> </span><span class="cbracket">{</span>
  158. <span class="normal"> </span><span class="comment">/* Report error to user, and free error */</span>
  159. <span class="normal"> </span><span class="function"><a href="glib-Testing.html#g-assert">g_assert</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">contents </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">);</span>
  160. <span class="normal"> </span><span class="function">fprintf</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">stderr</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"Unable to read file: %s</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> err</span><span class="symbol">-&gt;</span><span class="normal">message</span><span class="symbol">);</span>
  161. <span class="normal"> </span><span class="function"><a href="glib-Error-Reporting.html#g-error-free">g_error_free</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err</span><span class="symbol">);</span>
  162. <span class="normal"> </span><span class="cbracket">}</span>
  163. <span class="keyword">else</span>
  164. <span class="normal"> </span><span class="cbracket">{</span>
  165. <span class="normal"> </span><span class="comment">/* Use file contents */</span>
  166. <span class="normal"> </span><span class="function"><a href="glib-Testing.html#g-assert">g_assert</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">contents </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">);</span>
  167. <span class="normal"> </span><span class="cbracket">}</span></pre></td>
  168. </tr>
  169. </tbody>
  170. </table>
  171. </div>
  172. <p>
  173. Note that <code class="literal">err != NULL</code> in this example is a
  174. <span class="emphasis"><em>reliable</em></span> indicator of whether
  175. <a class="link" href="glib-File-Utilities.html#g-file-get-contents" title="g_file_get_contents ()"><code class="function">g_file_get_contents()</code></a> failed. Additionally, <a class="link" href="glib-File-Utilities.html#g-file-get-contents" title="g_file_get_contents ()"><code class="function">g_file_get_contents()</code></a> returns
  176. a boolean which indicates whether it was successful.
  177. </p>
  178. <p>
  179. Because <a class="link" href="glib-File-Utilities.html#g-file-get-contents" title="g_file_get_contents ()"><code class="function">g_file_get_contents()</code></a> returns <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> on failure, if you are only
  180. interested in whether it failed and don't need to display an error message, you
  181. can pass <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> for the <code class="literal">error</code> argument:
  182. </p>
  183. <div class="informalexample">
  184. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  185. <tbody>
  186. <tr>
  187. <td class="listing_lines" align="right"><pre>1
  188. 2
  189. 3
  190. 4</pre></td>
  191. <td class="listing_code"><pre class="programlisting"><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function"><a href="glib-File-Utilities.html#g-file-get-contents">g_file_get_contents</a></span><span class="normal"> </span><span class="symbol">(</span><span class="string">"foo.txt"</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">&amp;</span><span class="normal">contents</span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">))</span><span class="normal"> </span><span class="comment">/* ignore errors */</span>
  192. <span class="normal"> </span><span class="comment">/* no error occurred */</span><span class="normal"> </span><span class="symbol">;</span>
  193. <span class="keyword">else</span>
  194. <span class="normal"> </span><span class="comment">/* error */</span><span class="normal"> </span><span class="symbol">;</span></pre></td>
  195. </tr>
  196. </tbody>
  197. </table>
  198. </div>
  199. <p>
  200. </p>
  201. <p>
  202. The <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> object contains three fields: <code class="literal">domain</code> indicates
  203. the module the error-reporting function is located in, <code class="literal">code</code>
  204. indicates the specific error that occurred, and <code class="literal">message</code> is a
  205. user-readable error message with as many details as possible. Several functions
  206. are provided to deal with an error received from a called function:
  207. <a class="link" href="glib-Error-Reporting.html#g-error-matches" title="g_error_matches ()"><code class="function">g_error_matches()</code></a> returns <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if the error matches a given domain and code,
  208. <a class="link" href="glib-Error-Reporting.html#g-propagate-error" title="g_propagate_error ()"><code class="function">g_propagate_error()</code></a> copies an error into an error location (so the calling
  209. function will receive it), and <a class="link" href="glib-Error-Reporting.html#g-clear-error" title="g_clear_error ()"><code class="function">g_clear_error()</code></a> clears an error location by
  210. freeing the error and resetting the location to <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>. To display an error to the
  211. user, simply display <code class="literal">error-&gt;message</code>, perhaps along with
  212. additional context known only to the calling function (the file being opened, or
  213. whatever -- though in the <a class="link" href="glib-File-Utilities.html#g-file-get-contents" title="g_file_get_contents ()"><code class="function">g_file_get_contents()</code></a> case,
  214. <code class="literal">error-&gt;message</code> already contains a filename).
  215. </p>
  216. <p>
  217. When implementing a function that can report errors, the basic tool is
  218. <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()"><code class="function">g_set_error()</code></a>. Typically, if a fatal error occurs you want to <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()"><code class="function">g_set_error()</code></a>,
  219. then return immediately. <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()"><code class="function">g_set_error()</code></a> does nothing if the error location passed
  220. to it is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>. Here's an example:
  221. </p>
  222. <div class="informalexample">
  223. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  224. <tbody>
  225. <tr>
  226. <td class="listing_lines" align="right"><pre>1
  227. 2
  228. 3
  229. 4
  230. 5
  231. 6
  232. 7
  233. 8
  234. 9
  235. 10
  236. 11
  237. 12
  238. 13
  239. 14
  240. 15
  241. 16
  242. 17</pre></td>
  243. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gint">gint</a></span>
  244. <span class="function">foo_open_file</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">error</span><span class="symbol">)</span>
  245. <span class="cbracket">{</span>
  246. <span class="normal"> </span><span class="usertype">gint</span><span class="normal"> fd</span><span class="symbol">;</span>
  247. <span class="normal"> fd </span><span class="symbol">=</span><span class="normal"> </span><span class="function">open</span><span class="normal"> </span><span class="symbol">(</span><span class="string">"file.txt"</span><span class="symbol">,</span><span class="normal"> O_RDONLY</span><span class="symbol">);</span>
  248. <span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">fd </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
  249. <span class="normal"> </span><span class="cbracket">{</span>
  250. <span class="normal"> </span><span class="function"><a href="glib-Error-Reporting.html#g-set-error">g_set_error</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">error</span><span class="symbol">,</span>
  251. <span class="normal"> FOO_ERROR</span><span class="symbol">,</span><span class="normal"> </span><span class="comment">/* error domain */</span>
  252. <span class="normal"> FOO_ERROR_BLAH</span><span class="symbol">,</span><span class="normal"> </span><span class="comment">/* error code */</span>
  253. <span class="normal"> </span><span class="string">"Failed to open file: %s"</span><span class="symbol">,</span><span class="normal"> </span><span class="comment">/* error message format string */</span>
  254. <span class="normal"> </span><span class="function"><a href="glib-String-Utility-Functions.html#g-strerror">g_strerror</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">errno</span><span class="symbol">));</span>
  255. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
  256. <span class="normal"> </span><span class="cbracket">}</span>
  257. <span class="normal"> </span><span class="keyword">else</span>
  258. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> fd</span><span class="symbol">;</span>
  259. <span class="cbracket">}</span></pre></td>
  260. </tr>
  261. </tbody>
  262. </table>
  263. </div>
  264. <p>
  265. </p>
  266. <p>
  267. Things are somewhat more complicated if you yourself call another function that
  268. can report a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>. If the sub-function indicates fatal errors in some way
  269. other than reporting a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>, such as by returning <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> on success, you can
  270. simply do the following:
  271. </p>
  272. <div class="informalexample">
  273. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  274. <tbody>
  275. <tr>
  276. <td class="listing_lines" align="right"><pre>1
  277. 2
  278. 3
  279. 4
  280. 5
  281. 6
  282. 7
  283. 8
  284. 9
  285. 10
  286. 11
  287. 12
  288. 13</pre></td>
  289. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  290. <span class="function">my_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">err</span><span class="symbol">)</span>
  291. <span class="cbracket">{</span>
  292. <span class="normal"> </span><span class="function"><a href="glib-Warnings-and-Assertions.html#g-return-val-if-fail">g_return_val_if_fail</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">);</span>
  293. <span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">sub_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err</span><span class="symbol">))</span>
  294. <span class="normal"> </span><span class="cbracket">{</span>
  295. <span class="normal"> </span><span class="comment">/* assert that error was set by the sub-function */</span>
  296. <span class="normal"> </span><span class="function"><a href="glib-Testing.html#g-assert">g_assert</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">);</span>
  297. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">;</span>
  298. <span class="normal"> </span><span class="cbracket">}</span>
  299. <span class="normal"> </span><span class="comment">/* otherwise continue, no error occurred */</span>
  300. <span class="normal"> </span><span class="function"><a href="glib-Testing.html#g-assert">g_assert</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">);</span>
  301. <span class="cbracket">}</span></pre></td>
  302. </tr>
  303. </tbody>
  304. </table>
  305. </div>
  306. <p>
  307. </p>
  308. <p>
  309. If the sub-function does not indicate errors other than by reporting a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>,
  310. you need to create a temporary <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> since the passed-in one may be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>.
  311. <a class="link" href="glib-Error-Reporting.html#g-propagate-error" title="g_propagate_error ()"><code class="function">g_propagate_error()</code></a> is intended for use in this case.
  312. </p>
  313. <div class="informalexample">
  314. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  315. <tbody>
  316. <tr>
  317. <td class="listing_lines" align="right"><pre>1
  318. 2
  319. 3
  320. 4
  321. 5
  322. 6
  323. 7
  324. 8
  325. 9
  326. 10
  327. 11
  328. 12
  329. 13
  330. 14
  331. 15
  332. 16
  333. 17</pre></td>
  334. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  335. <span class="function">my_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">err</span><span class="symbol">)</span>
  336. <span class="cbracket">{</span>
  337. <span class="normal"> </span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">tmp_error</span><span class="symbol">;</span>
  338. <span class="normal"> </span><span class="function"><a href="glib-Warnings-and-Assertions.html#g-return-val-if-fail">g_return_val_if_fail</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">);</span>
  339. <span class="normal"> tmp_error </span><span class="symbol">=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">;</span>
  340. <span class="normal"> </span><span class="function">sub_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(&amp;</span><span class="normal">tmp_error</span><span class="symbol">);</span>
  341. <span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">tmp_error </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">)</span>
  342. <span class="normal"> </span><span class="cbracket">{</span>
  343. <span class="normal"> </span><span class="comment">/* store tmp_error in err, if err != NULL,</span>
  344. <span class="comment"> * otherwise call g_error_free() on tmp_error</span>
  345. <span class="comment"> */</span>
  346. <span class="normal"> </span><span class="function"><a href="glib-Error-Reporting.html#g-propagate-error">g_propagate_error</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err</span><span class="symbol">,</span><span class="normal"> tmp_error</span><span class="symbol">);</span>
  347. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">;</span>
  348. <span class="normal"> </span><span class="cbracket">}</span>
  349. <span class="normal"> </span><span class="comment">/* otherwise continue, no error occurred */</span>
  350. <span class="cbracket">}</span></pre></td>
  351. </tr>
  352. </tbody>
  353. </table>
  354. </div>
  355. <p>
  356. </p>
  357. <p>
  358. Error pileups are always a bug. For example, this code is incorrect:
  359. </p>
  360. <div class="informalexample">
  361. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  362. <tbody>
  363. <tr>
  364. <td class="listing_lines" align="right"><pre>1
  365. 2
  366. 3
  367. 4
  368. 5
  369. 6
  370. 7
  371. 8
  372. 9
  373. 10
  374. 11
  375. 12
  376. 13
  377. 14</pre></td>
  378. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  379. <span class="function">my_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">err</span><span class="symbol">)</span>
  380. <span class="cbracket">{</span>
  381. <span class="normal"> </span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">tmp_error</span><span class="symbol">;</span>
  382. <span class="normal"> </span><span class="function"><a href="glib-Warnings-and-Assertions.html#g-return-val-if-fail">g_return_val_if_fail</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">);</span>
  383. <span class="normal"> tmp_error </span><span class="symbol">=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">;</span>
  384. <span class="normal"> </span><span class="function">sub_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(&amp;</span><span class="normal">tmp_error</span><span class="symbol">);</span>
  385. <span class="normal"> </span><span class="function">other_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(&amp;</span><span class="normal">tmp_error</span><span class="symbol">);</span>
  386. <span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">tmp_error </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">)</span>
  387. <span class="normal"> </span><span class="cbracket">{</span>
  388. <span class="normal"> </span><span class="function"><a href="glib-Error-Reporting.html#g-propagate-error">g_propagate_error</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err</span><span class="symbol">,</span><span class="normal"> tmp_error</span><span class="symbol">);</span>
  389. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">;</span>
  390. <span class="normal"> </span><span class="cbracket">}</span>
  391. <span class="cbracket">}</span></pre></td>
  392. </tr>
  393. </tbody>
  394. </table>
  395. </div>
  396. <p>
  397. <code class="literal">tmp_error</code> should be checked immediately after
  398. <code class="function"><code class="function">sub_function_that_can_fail()</code></code>, and either cleared or propagated upward. The rule
  399. is: <span class="emphasis"><em>after each error, you must either handle the error, or return it to the
  400. calling function</em></span>. Note that passing <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> for the error location is the
  401. equivalent of handling an error by always doing nothing about it. So the
  402. following code is fine, assuming errors in <code class="function"><code class="function">sub_function_that_can_fail()</code></code> are not
  403. fatal to <code class="function"><code class="function">my_function_that_can_fail()</code></code>:
  404. </p>
  405. <div class="informalexample">
  406. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  407. <tbody>
  408. <tr>
  409. <td class="listing_lines" align="right"><pre>1
  410. 2
  411. 3
  412. 4
  413. 5
  414. 6
  415. 7
  416. 8
  417. 9
  418. 10
  419. 11
  420. 12
  421. 13
  422. 14</pre></td>
  423. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  424. <span class="function">my_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">**</span><span class="normal">err</span><span class="symbol">)</span>
  425. <span class="cbracket">{</span>
  426. <span class="normal"> </span><span class="usertype">GError</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">tmp_error</span><span class="symbol">;</span>
  427. <span class="normal"> </span><span class="function"><a href="glib-Warnings-and-Assertions.html#g-return-val-if-fail">g_return_val_if_fail</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">err </span><span class="symbol">==</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">);</span>
  428. <span class="normal"> </span><span class="function">sub_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(</span><span class="normal"><a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">);</span><span class="normal"> </span><span class="comment">/* ignore errors */</span>
  429. <span class="normal"> tmp_error </span><span class="symbol">=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">;</span>
  430. <span class="normal"> </span><span class="function">other_function_that_can_fail</span><span class="normal"> </span><span class="symbol">(&amp;</span><span class="normal">tmp_error</span><span class="symbol">);</span>
  431. <span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">tmp_error </span><span class="symbol">!=</span><span class="normal"> <a href="glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">)</span>
  432. <span class="normal"> </span><span class="cbracket">{</span>
  433. <span class="normal"> </span><span class="function"><a href="glib-Error-Reporting.html#g-propagate-error">g_propagate_error</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">err</span><span class="symbol">,</span><span class="normal"> tmp_error</span><span class="symbol">);</span>
  434. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> <a href="glib-Standard-Macros.html#FALSE:CAPS">FALSE</a></span><span class="symbol">;</span>
  435. <span class="normal"> </span><span class="cbracket">}</span>
  436. <span class="cbracket">}</span></pre></td>
  437. </tr>
  438. </tbody>
  439. </table>
  440. </div>
  441. <p>
  442. </p>
  443. <p>
  444. Note that passing <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> for the error location <span class="emphasis"><em>ignores</em></span>
  445. errors; it's equivalent to <code class="literal">try { <code class="function">sub_function_that_can_fail()</code>; } catch
  446. (...) {}</code> in C++. It does <span class="emphasis"><em>not</em></span> mean to leave errors
  447. unhandled; it means to handle them by doing nothing.
  448. </p>
  449. <p>
  450. Error domains and codes are conventionally named as follows:
  451. </p>
  452. <div class="itemizedlist"><ul class="itemizedlist" type="disc">
  453. <li class="listitem">
  454. <p>
  455. The error domain is called
  456. <code class="literal">&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR</code>, for example
  457. <a class="link" href="glib-Spawning-Processes.html#G-SPAWN-ERROR:CAPS" title="G_SPAWN_ERROR"><code class="literal">G_SPAWN_ERROR</code></a> or <a class="link" href="glib-Threads.html#G-THREAD-ERROR:CAPS" title="G_THREAD_ERROR"><code class="literal">G_THREAD_ERROR</code></a>:
  458. </p>
  459. <div class="informalexample">
  460. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  461. <tbody>
  462. <tr>
  463. <td class="listing_lines" align="right"><pre>1
  464. 2
  465. 3
  466. 4
  467. 5
  468. 6</pre></td>
  469. <td class="listing_code"><pre class="programlisting"><span class="preproc">#define</span><span class="normal"> </span><span class="usertype">G_SPAWN_ERROR</span><span class="normal"> </span><span class="function">g_spawn_error_quark</span><span class="normal"> </span><span class="symbol">()</span>
  470. <span class="normal"><a href="glib-Quarks.html#GQuark">GQuark</a></span>
  471. <span class="function">g_spawn_error_quark</span><span class="normal"> </span><span class="symbol">(</span><span class="type">void</span><span class="symbol">)</span>
  472. <span class="cbracket">{</span>
  473. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function"><a href="glib-Quarks.html#g-quark-from-static-string">g_quark_from_static_string</a></span><span class="normal"> </span><span class="symbol">(</span><span class="string">"g-spawn-error-quark"</span><span class="symbol">);</span>
  474. <span class="cbracket">}</span></pre></td>
  475. </tr>
  476. </tbody>
  477. </table>
  478. </div>
  479. <p>
  480. </p>
  481. </li>
  482. <li class="listitem"><p>
  483. The quark function for the error domain is called <code class="literal">&lt;namespace&gt;_&lt;module&gt;_error_quark</code>, for example <code class="function">g_spawn_error_quark()</code> or %<code class="function">g_thread_error_quark()</code>.
  484. </p></li>
  485. <li class="listitem"><p>
  486. The error codes are in an enumeration called
  487. <code class="literal">&lt;Namespace&gt;&lt;Module&gt;Error</code>; for example,
  488. <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>.
  489. </p></li>
  490. <li class="listitem"><p>
  491. 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>.
  492. </p></li>
  493. <li class="listitem"><p>
  494. If there's a "generic" or "unknown" error code for unrecoverable errors it
  495. doesn't make sense to distinguish with specific codes, it should be called
  496. <code class="literal">&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_FAILED</code>, for
  497. example <a class="link" href="glib-Spawning-Processes.html#G-SPAWN-ERROR-FAILED:CAPS"><code class="literal">G_SPAWN_ERROR_FAILED</code></a> or <code class="literal">G_THREAD_ERROR_FAILED</code>.
  498. </p></li>
  499. </ul></div>
  500. <p>
  501. </p>
  502. <p>
  503. Summary of rules for use of <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>:
  504. </p>
  505. <div class="itemizedlist"><ul class="itemizedlist" type="disc">
  506. <li class="listitem"><p>
  507. Do not report programming errors via <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>.
  508. </p></li>
  509. <li class="listitem"><p>
  510. The last argument of a function that returns an error should be a
  511. 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
  512. <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
  513. argument before the "...".
  514. </p></li>
  515. <li class="listitem"><p>
  516. 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
  517. in details of the exact error that occurred.
  518. </p></li>
  519. <li class="listitem"><p>
  520. 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
  521. not be returned to the caller, but your function should still
  522. abort and return if an error occurs. That is, control flow should
  523. 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>.
  524. </p></li>
  525. <li class="listitem"><p>
  526. 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
  527. <span class="emphasis"><em>had a fatal failure and did not complete whatever it was supposed
  528. to do</em></span>. If the failure was not fatal, then you handled it
  529. and you should not report it. If it was fatal, then you must report it
  530. and discontinue whatever you were doing immediately.
  531. </p></li>
  532. <li class="listitem"><p>
  533. 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
  534. a function that can report errors.
  535. </p></li>
  536. <li class="listitem"><p>
  537. "Piling up" errors is always a bug. That is, if you assign a new
  538. <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
  539. error, it indicates that you should have aborted the operation instead
  540. of continuing. If you were able to continue, you should have cleared
  541. 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
  542. if you pile up errors.
  543. </p></li>
  544. <li class="listitem"><p>
  545. By convention, if you return a boolean value indicating success
  546. 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,
  547. 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.
  548. </p></li>
  549. <li class="listitem"><p>
  550. 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
  551. 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
  552. 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,
  553. then users must check whether an error was returned to see if the
  554. function succeeded.
  555. </p></li>
  556. <li class="listitem"><p>
  557. When implementing a function that can report errors, you may want to
  558. add a check at the top of your function that the error return location
  559. 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
  560. (e.g. <code class="literal">g_return_if_fail (error == NULL || *error ==
  561. NULL);</code>).
  562. </p></li>
  563. </ul></div>
  564. <p>
  565. </p>
  566. </div>
  567. <div class="refsect1">
  568. <a name="glib-Error-Reporting.details"></a><h2>Details</h2>
  569. <div class="refsect2">
  570. <a name="GError"></a><h3>GError</h3>
  571. <pre class="programlisting">typedef struct {
  572. GQuark domain;
  573. gint code;
  574. gchar *message;
  575. } GError;
  576. </pre>
  577. <p>
  578. The <span class="structname">GError</span> structure contains
  579. information about an error that has occurred.
  580. </p>
  581. <div class="variablelist"><table border="0">
  582. <col align="left" valign="top">
  583. <tbody>
  584. <tr>
  585. <td><p><span class="term"><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> <em class="structfield"><code><a name="GError.domain"></a>domain</code></em>;</span></p></td>
  586. <td>error domain, e.g. <a class="link" href="glib-File-Utilities.html#G-FILE-ERROR:CAPS" title="G_FILE_ERROR"><span class="type">G_FILE_ERROR</span></a>.
  587. </td>
  588. </tr>
  589. <tr>
  590. <td><p><span class="term"><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> <em class="structfield"><code><a name="GError.code"></a>code</code></em>;</span></p></td>
  591. <td>error code, e.g. <a class="link" href="glib-File-Utilities.html#G-FILE-ERROR-NOENT:CAPS"><code class="literal">G_FILE_ERROR_NOENT</code></a>.
  592. </td>
  593. </tr>
  594. <tr>
  595. <td><p><span class="term"><a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *<em class="structfield"><code><a name="GError.message"></a>message</code></em>;</span></p></td>
  596. <td>human-readable informative error message.
  597. </td>
  598. </tr>
  599. </tbody>
  600. </table></div>
  601. </div>
  602. <hr>
  603. <div class="refsect2">
  604. <a name="g-error-new"></a><h3>g_error_new ()</h3>
  605. <pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * g_error_new (<em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  606. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  607. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  608. <em class="parameter"><code>...</code></em>);</pre>
  609. <p>
  610. Creates a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> with the given <em class="parameter"><code>domain</code></em> and <em class="parameter"><code>code</code></em>,
  611. and a message formatted with <em class="parameter"><code>format</code></em>.
  612. </p>
  613. <div class="variablelist"><table border="0">
  614. <col align="left" valign="top">
  615. <tbody>
  616. <tr>
  617. <td><p><span class="term"><em class="parameter"><code>domain</code></em> :</span></p></td>
  618. <td>error domain
  619. </td>
  620. </tr>
  621. <tr>
  622. <td><p><span class="term"><em class="parameter"><code>code</code></em> :</span></p></td>
  623. <td>error code
  624. </td>
  625. </tr>
  626. <tr>
  627. <td><p><span class="term"><em class="parameter"><code>format</code></em> :</span></p></td>
  628. <td>printf()-style format for error message
  629. </td>
  630. </tr>
  631. <tr>
  632. <td><p><span class="term"><em class="parameter"><code>...</code></em> :</span></p></td>
  633. <td>parameters for message format
  634. </td>
  635. </tr>
  636. <tr>
  637. <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
  638. <td> a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>
  639. </td>
  640. </tr>
  641. </tbody>
  642. </table></div>
  643. </div>
  644. <hr>
  645. <div class="refsect2">
  646. <a name="g-error-new-literal"></a><h3>g_error_new_literal ()</h3>
  647. <pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * g_error_new_literal (<em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  648. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  649. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *message</code></em>);</pre>
  650. <p>
  651. Creates a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>; unlike <a class="link" href="glib-Error-Reporting.html#g-error-new" title="g_error_new ()"><code class="function">g_error_new()</code></a>, <em class="parameter"><code>message</code></em> is
  652. not a <code class="function">printf()</code>-style format string. Use this function if
  653. <em class="parameter"><code>message</code></em> contains text you don't have control over,
  654. that could include <code class="function">printf()</code> escape sequences.
  655. </p>
  656. <div class="variablelist"><table border="0">
  657. <col align="left" valign="top">
  658. <tbody>
  659. <tr>
  660. <td><p><span class="term"><em class="parameter"><code>domain</code></em> :</span></p></td>
  661. <td>error domain
  662. </td>
  663. </tr>
  664. <tr>
  665. <td><p><span class="term"><em class="parameter"><code>code</code></em> :</span></p></td>
  666. <td>error code
  667. </td>
  668. </tr>
  669. <tr>
  670. <td><p><span class="term"><em class="parameter"><code>message</code></em> :</span></p></td>
  671. <td>error message
  672. </td>
  673. </tr>
  674. <tr>
  675. <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
  676. <td> a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>
  677. </td>
  678. </tr>
  679. </tbody>
  680. </table></div>
  681. </div>
  682. <hr>
  683. <div class="refsect2">
  684. <a name="g-error-new-valist"></a><h3>g_error_new_valist ()</h3>
  685. <pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * g_error_new_valist (<em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  686. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  687. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  688. <em class="parameter"><code><span class="type">va_list</span> args</code></em>);</pre>
  689. <p>
  690. Creates a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> with the given <em class="parameter"><code>domain</code></em> and <em class="parameter"><code>code</code></em>,
  691. and a message formatted with <em class="parameter"><code>format</code></em>.
  692. </p>
  693. <div class="variablelist"><table border="0">
  694. <col align="left" valign="top">
  695. <tbody>
  696. <tr>
  697. <td><p><span class="term"><em class="parameter"><code>domain</code></em> :</span></p></td>
  698. <td>error domain
  699. </td>
  700. </tr>
  701. <tr>
  702. <td><p><span class="term"><em class="parameter"><code>code</code></em> :</span></p></td>
  703. <td>error code
  704. </td>
  705. </tr>
  706. <tr>
  707. <td><p><span class="term"><em class="parameter"><code>format</code></em> :</span></p></td>
  708. <td>printf()-style format for error message
  709. </td>
  710. </tr>
  711. <tr>
  712. <td><p><span class="term"><em class="parameter"><code>args</code></em> :</span></p></td>
  713. <td>
  714. <span class="type">va_list</span> of parameters for the message format
  715. </td>
  716. </tr>
  717. <tr>
  718. <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
  719. <td> a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>
  720. </td>
  721. </tr>
  722. </tbody>
  723. </table></div>
  724. <p class="since">Since 2.22</p>
  725. </div>
  726. <hr>
  727. <div class="refsect2">
  728. <a name="g-error-free"></a><h3>g_error_free ()</h3>
  729. <pre class="programlisting"><span class="returnvalue">void</span> g_error_free (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *error</code></em>);</pre>
  730. <p>
  731. Frees a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> and associated resources.
  732. </p>
  733. <div class="variablelist"><table border="0">
  734. <col align="left" valign="top">
  735. <tbody><tr>
  736. <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
  737. <td>a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>
  738. </td>
  739. </tr></tbody>
  740. </table></div>
  741. </div>
  742. <hr>
  743. <div class="refsect2">
  744. <a name="g-error-copy"></a><h3>g_error_copy ()</h3>
  745. <pre class="programlisting"><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="returnvalue">GError</span></a> * g_error_copy (<em class="parameter"><code>const <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *error</code></em>);</pre>
  746. <p>
  747. Makes a copy of <em class="parameter"><code>error</code></em>.
  748. </p>
  749. <div class="variablelist"><table border="0">
  750. <col align="left" valign="top">
  751. <tbody>
  752. <tr>
  753. <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
  754. <td>a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>
  755. </td>
  756. </tr>
  757. <tr>
  758. <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
  759. <td> a new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>
  760. </td>
  761. </tr>
  762. </tbody>
  763. </table></div>
  764. </div>
  765. <hr>
  766. <div class="refsect2">
  767. <a name="g-error-matches"></a><h3>g_error_matches ()</h3>
  768. <pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a> g_error_matches (<em class="parameter"><code>const <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *error</code></em>,
  769. <em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  770. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>);</pre>
  771. <p>
  772. Returns <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if <em class="parameter"><code>error</code></em> matches <em class="parameter"><code>domain</code></em> and <em class="parameter"><code>code</code></em>, <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a>
  773. otherwise. In particular, when <em class="parameter"><code>error</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>, <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> will
  774. be returned.
  775. </p>
  776. <div class="variablelist"><table border="0">
  777. <col align="left" valign="top">
  778. <tbody>
  779. <tr>
  780. <td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
  781. <td>a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>
  782. </td>
  783. </tr>
  784. <tr>
  785. <td><p><span class="term"><em class="parameter"><code>domain</code></em> :</span></p></td>
  786. <td>an error domain
  787. </td>
  788. </tr>
  789. <tr>
  790. <td><p><span class="term"><em class="parameter"><code>code</code></em> :</span></p></td>
  791. <td>an error code
  792. </td>
  793. </tr>
  794. <tr>
  795. <td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
  796. <td> whether <em class="parameter"><code>error</code></em> has <em class="parameter"><code>domain</code></em> and <em class="parameter"><code>code</code></em>
  797. </td>
  798. </tr>
  799. </tbody>
  800. </table></div>
  801. </div>
  802. <hr>
  803. <div class="refsect2">
  804. <a name="g-set-error"></a><h3>g_set_error ()</h3>
  805. <pre class="programlisting"><span class="returnvalue">void</span> g_set_error (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>,
  806. <em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  807. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  808. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  809. <em class="parameter"><code>...</code></em>);</pre>
  810. <p>
  811. Does nothing if <em class="parameter"><code>err</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>; if <em class="parameter"><code>err</code></em> is non-<a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>, then *<em class="parameter"><code>err</code></em>
  812. must be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>. A new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> is created and assigned to *<em class="parameter"><code>err</code></em>.
  813. </p>
  814. <div class="variablelist"><table border="0">
  815. <col align="left" valign="top">
  816. <tbody>
  817. <tr>
  818. <td><p><span class="term"><em class="parameter"><code>err</code></em> :</span></p></td>
  819. <td>a return location for a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>
  820. </td>
  821. </tr>
  822. <tr>
  823. <td><p><span class="term"><em class="parameter"><code>domain</code></em> :</span></p></td>
  824. <td>error domain
  825. </td>
  826. </tr>
  827. <tr>
  828. <td><p><span class="term"><em class="parameter"><code>code</code></em> :</span></p></td>
  829. <td>error code
  830. </td>
  831. </tr>
  832. <tr>
  833. <td><p><span class="term"><em class="parameter"><code>format</code></em> :</span></p></td>
  834. <td>printf()-style format
  835. </td>
  836. </tr>
  837. <tr>
  838. <td><p><span class="term"><em class="parameter"><code>...</code></em> :</span></p></td>
  839. <td>args for <em class="parameter"><code>format</code></em>
  840. </td>
  841. </tr>
  842. </tbody>
  843. </table></div>
  844. </div>
  845. <hr>
  846. <div class="refsect2">
  847. <a name="g-set-error-literal"></a><h3>g_set_error_literal ()</h3>
  848. <pre class="programlisting"><span class="returnvalue">void</span> g_set_error_literal (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>,
  849. <em class="parameter"><code><a class="link" href="glib-Quarks.html#GQuark" title="GQuark"><span class="type">GQuark</span></a> domain</code></em>,
  850. <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> code</code></em>,
  851. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *message</code></em>);</pre>
  852. <p>
  853. Does nothing if <em class="parameter"><code>err</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>; if <em class="parameter"><code>err</code></em> is non-<a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>, then *<em class="parameter"><code>err</code></em>
  854. must be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>. A new <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> is created and assigned to *<em class="parameter"><code>err</code></em>.
  855. Unlike <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()"><code class="function">g_set_error()</code></a>, <em class="parameter"><code>message</code></em> is not a <code class="function">printf()</code>-style format string.
  856. Use this function if <em class="parameter"><code>message</code></em> contains text you don't have control over,
  857. that could include <code class="function">printf()</code> escape sequences.
  858. </p>
  859. <div class="variablelist"><table border="0">
  860. <col align="left" valign="top">
  861. <tbody>
  862. <tr>
  863. <td><p><span class="term"><em class="parameter"><code>err</code></em> :</span></p></td>
  864. <td>a return location for a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>
  865. </td>
  866. </tr>
  867. <tr>
  868. <td><p><span class="term"><em class="parameter"><code>domain</code></em> :</span></p></td>
  869. <td>error domain
  870. </td>
  871. </tr>
  872. <tr>
  873. <td><p><span class="term"><em class="parameter"><code>code</code></em> :</span></p></td>
  874. <td>error code
  875. </td>
  876. </tr>
  877. <tr>
  878. <td><p><span class="term"><em class="parameter"><code>message</code></em> :</span></p></td>
  879. <td>error message
  880. </td>
  881. </tr>
  882. </tbody>
  883. </table></div>
  884. <p class="since">Since 2.18</p>
  885. </div>
  886. <hr>
  887. <div class="refsect2">
  888. <a name="g-propagate-error"></a><h3>g_propagate_error ()</h3>
  889. <pre class="programlisting"><span class="returnvalue">void</span> g_propagate_error (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **dest</code></em>,
  890. <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *src</code></em>);</pre>
  891. <p>
  892. If <em class="parameter"><code>dest</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>, free <em class="parameter"><code>src</code></em>; otherwise, moves <em class="parameter"><code>src</code></em> into *<em class="parameter"><code>dest</code></em>.
  893. The error variable <em class="parameter"><code>dest</code></em> points to must be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>.
  894. </p>
  895. <div class="variablelist"><table border="0">
  896. <col align="left" valign="top">
  897. <tbody>
  898. <tr>
  899. <td><p><span class="term"><em class="parameter"><code>dest</code></em> :</span></p></td>
  900. <td>error return location
  901. </td>
  902. </tr>
  903. <tr>
  904. <td><p><span class="term"><em class="parameter"><code>src</code></em> :</span></p></td>
  905. <td>error to move into the return location
  906. </td>
  907. </tr>
  908. </tbody>
  909. </table></div>
  910. </div>
  911. <hr>
  912. <div class="refsect2">
  913. <a name="g-clear-error"></a><h3>g_clear_error ()</h3>
  914. <pre class="programlisting"><span class="returnvalue">void</span> g_clear_error (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>);</pre>
  915. <p>
  916. If <em class="parameter"><code>err</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>, does nothing. If <em class="parameter"><code>err</code></em> is non-<a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>,
  917. calls <a class="link" href="glib-Error-Reporting.html#g-error-free" title="g_error_free ()"><code class="function">g_error_free()</code></a> on *<em class="parameter"><code>err</code></em> and sets *<em class="parameter"><code>err</code></em> to <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>.
  918. </p>
  919. <div class="variablelist"><table border="0">
  920. <col align="left" valign="top">
  921. <tbody><tr>
  922. <td><p><span class="term"><em class="parameter"><code>err</code></em> :</span></p></td>
  923. <td>a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> return location
  924. </td>
  925. </tr></tbody>
  926. </table></div>
  927. </div>
  928. <hr>
  929. <div class="refsect2">
  930. <a name="g-prefix-error"></a><h3>g_prefix_error ()</h3>
  931. <pre class="programlisting"><span class="returnvalue">void</span> g_prefix_error (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **err</code></em>,
  932. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  933. <em class="parameter"><code>...</code></em>);</pre>
  934. <p>
  935. Formats a string according to <em class="parameter"><code>format</code></em> and
  936. prefix it to an existing error message. If
  937. <em class="parameter"><code>err</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> (ie: no error variable) then do
  938. nothing.
  939. </p>
  940. <p>
  941. If *<em class="parameter"><code>err</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> (ie: an error variable is
  942. present but there is no error condition) then
  943. also do nothing. Whether or not it makes
  944. sense to take advantage of this feature is up
  945. to you.
  946. </p>
  947. <div class="variablelist"><table border="0">
  948. <col align="left" valign="top">
  949. <tbody>
  950. <tr>
  951. <td><p><span class="term"><em class="parameter"><code>err</code></em> :</span></p></td>
  952. <td>a return location for a <a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a>, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>
  953. </td>
  954. </tr>
  955. <tr>
  956. <td><p><span class="term"><em class="parameter"><code>format</code></em> :</span></p></td>
  957. <td>printf()-style format string
  958. </td>
  959. </tr>
  960. <tr>
  961. <td><p><span class="term"><em class="parameter"><code>...</code></em> :</span></p></td>
  962. <td>arguments to <em class="parameter"><code>format</code></em>
  963. </td>
  964. </tr>
  965. </tbody>
  966. </table></div>
  967. <p class="since">Since 2.16</p>
  968. </div>
  969. <hr>
  970. <div class="refsect2">
  971. <a name="g-propagate-prefixed-error"></a><h3>g_propagate_prefixed_error ()</h3>
  972. <pre class="programlisting"><span class="returnvalue">void</span> g_propagate_prefixed_error (<em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> **dest</code></em>,
  973. <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="GError"><span class="type">GError</span></a> *src</code></em>,
  974. <em class="parameter"><code>const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar"><span class="type">gchar</span></a> *format</code></em>,
  975. <em class="parameter"><code>...</code></em>);</pre>
  976. <p>
  977. If <em class="parameter"><code>dest</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>, free <em class="parameter"><code>src</code></em>; otherwise,
  978. moves <em class="parameter"><code>src</code></em> into *<em class="parameter"><code>dest</code></em>. *<em class="parameter"><code>dest</code></em> must be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>.
  979. After the move, add a prefix as with
  980. <a class="link" href="glib-Error-Reporting.html#g-prefix-error" title="g_prefix_error ()"><code class="function">g_prefix_error()</code></a>.
  981. </p>
  982. <div class="variablelist"><table border="0">
  983. <col align="left" valign="top">
  984. <tbody>
  985. <tr>
  986. <td><p><span class="term"><em class="parameter"><code>dest</code></em> :</span></p></td>
  987. <td>error return location
  988. </td>
  989. </tr>
  990. <tr>
  991. <td><p><span class="term"><em class="parameter"><code>src</code></em> :</span></p></td>
  992. <td>error to move into the return location
  993. </td>
  994. </tr>
  995. <tr>
  996. <td><p><span class="term"><em class="parameter"><code>format</code></em> :</span></p></td>
  997. <td>printf()-style format string
  998. </td>
  999. </tr>
  1000. <tr>
  1001. <td><p><span class="term"><em class="parameter"><code>...</code></em> :</span></p></td>
  1002. <td>arguments to <em class="parameter"><code>format</code></em>
  1003. </td>
  1004. </tr>
  1005. </tbody>
  1006. </table></div>
  1007. <p class="since">Since 2.16</p>
  1008. </div>
  1009. </div>
  1010. </div>
  1011. <div class="footer">
  1012. <hr>
  1013. Generated by GTK-Doc V1.15.1</div>
  1014. </body>
  1015. </html>