PageRenderTime 195ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/kokua/3p-glib
HTML | 1232 lines | 1204 code | 28 blank | 0 comment | 0 complexity | ca0d5e392677eda092891cc5ca04e263 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  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: GLib Reference Manual</title>
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
  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.21.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="5"><tr valign="middle">
  16. <td width="100%" align="left" class="shortcuts">
  17. <a href="#" class="shortcut">Top</a><span id="nav_description">  <span class="dim">|</span> 
  18. <a href="#glib-Error-Reporting.description" class="shortcut">Description</a></span>
  19. </td>
  20. <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
  21. <td><a accesskey="u" href="glib-core.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
  22. <td><a accesskey="p" href="glib-IO-Channels.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
  23. <td><a accesskey="n" href="glib-Warnings-and-Assertions.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
  24. </tr></table>
  25. <div class="refentry">
  26. <a name="glib-Error-Reporting"></a><div class="titlepage"></div>
  27. <div class="refnamediv"><table width="100%"><tr>
  28. <td valign="top">
  29. <h2><span class="refentrytitle"><a name="glib-Error-Reporting.top_of_page"></a>Error Reporting</span></h2>
  30. <p>Error Reporting a system for reporting errors</p>
  31. </td>
  32. <td class="gallery_image" valign="top" align="right"></td>
  33. </tr></table></div>
  34. <div class="refsect1">
  35. <a name="glib-Error-Reporting.functions"></a><h2>Functions</h2>
  36. <div class="informaltable"><table width="100%" border="0">
  37. <colgroup>
  38. <col width="150px" class="functions_return">
  39. <col class="functions_name">
  40. </colgroup>
  41. <tbody>
  42. <tr>
  43. <td class="function_type">
  44. <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="returnvalue">GError</span></a> *
  45. </td>
  46. <td class="function_name">
  47. <a class="link" href="glib-Error-Reporting.html#g-error-new" title="g_error_new ()">g_error_new</a> <span class="c_punctuation">()</span>
  48. </td>
  49. </tr>
  50. <tr>
  51. <td class="function_type">
  52. <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="returnvalue">GError</span></a> *
  53. </td>
  54. <td class="function_name">
  55. <a class="link" href="glib-Error-Reporting.html#g-error-new-literal" title="g_error_new_literal ()">g_error_new_literal</a> <span class="c_punctuation">()</span>
  56. </td>
  57. </tr>
  58. <tr>
  59. <td class="function_type">
  60. <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="returnvalue">GError</span></a> *
  61. </td>
  62. <td class="function_name">
  63. <a class="link" href="glib-Error-Reporting.html#g-error-new-valist" title="g_error_new_valist ()">g_error_new_valist</a> <span class="c_punctuation">()</span>
  64. </td>
  65. </tr>
  66. <tr>
  67. <td class="function_type">
  68. <span class="returnvalue">void</span>
  69. </td>
  70. <td class="function_name">
  71. <a class="link" href="glib-Error-Reporting.html#g-error-free" title="g_error_free ()">g_error_free</a> <span class="c_punctuation">()</span>
  72. </td>
  73. </tr>
  74. <tr>
  75. <td class="function_type">
  76. <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="returnvalue">GError</span></a> *
  77. </td>
  78. <td class="function_name">
  79. <a class="link" href="glib-Error-Reporting.html#g-error-copy" title="g_error_copy ()">g_error_copy</a> <span class="c_punctuation">()</span>
  80. </td>
  81. </tr>
  82. <tr>
  83. <td class="function_type">
  84. <a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a>
  85. </td>
  86. <td class="function_name">
  87. <a class="link" href="glib-Error-Reporting.html#g-error-matches" title="g_error_matches ()">g_error_matches</a> <span class="c_punctuation">()</span>
  88. </td>
  89. </tr>
  90. <tr>
  91. <td class="function_type">
  92. <span class="returnvalue">void</span>
  93. </td>
  94. <td class="function_name">
  95. <a class="link" href="glib-Error-Reporting.html#g-set-error" title="g_set_error ()">g_set_error</a> <span class="c_punctuation">()</span>
  96. </td>
  97. </tr>
  98. <tr>
  99. <td class="function_type">
  100. <span class="returnvalue">void</span>
  101. </td>
  102. <td class="function_name">
  103. <a class="link" href="glib-Error-Reporting.html#g-set-error-literal" title="g_set_error_literal ()">g_set_error_literal</a> <span class="c_punctuation">()</span>
  104. </td>
  105. </tr>
  106. <tr>
  107. <td class="function_type">
  108. <span class="returnvalue">void</span>
  109. </td>
  110. <td class="function_name">
  111. <a class="link" href="glib-Error-Reporting.html#g-propagate-error" title="g_propagate_error ()">g_propagate_error</a> <span class="c_punctuation">()</span>
  112. </td>
  113. </tr>
  114. <tr>
  115. <td class="function_type">
  116. <span class="returnvalue">void</span>
  117. </td>
  118. <td class="function_name">
  119. <a class="link" href="glib-Error-Reporting.html#g-clear-error" title="g_clear_error ()">g_clear_error</a> <span class="c_punctuation">()</span>
  120. </td>
  121. </tr>
  122. <tr>
  123. <td class="function_type">
  124. <span class="returnvalue">void</span>
  125. </td>
  126. <td class="function_name">
  127. <a class="link" href="glib-Error-Reporting.html#g-prefix-error" title="g_prefix_error ()">g_prefix_error</a> <span class="c_punctuation">()</span>
  128. </td>
  129. </tr>
  130. <tr>
  131. <td class="function_type">
  132. <span class="returnvalue">void</span>
  133. </td>
  134. <td class="function_name">
  135. <a class="link" href="glib-Error-Reporting.html#g-propagate-prefixed-error" title="g_propagate_prefixed_error ()">g_propagate_prefixed_error</a> <span class="c_punctuation">()</span>
  136. </td>
  137. </tr>
  138. </tbody>
  139. </table></div>
  140. </div>
  141. <div class="refsect1">
  142. <a name="glib-Error-Reporting.other"></a><h2>Types and Values</h2>
  143. <div class="informaltable"><table width="100%" border="0">
  144. <colgroup>
  145. <col width="150px" class="name">
  146. <col class="description">
  147. </colgroup>
  148. <tbody><tr>
  149. <td class="datatype_keyword">struct</td>
  150. <td class="function_name"><a class="link" href="glib-Error-Reporting.html#GError" title="struct GError">GError</a></td>
  151. </tr></tbody>
  152. </table></div>
  153. </div>
  154. <div class="refsect1">
  155. <a name="glib-Error-Reporting.includes"></a><h2>Includes</h2>
  156. <pre class="synopsis">#include &lt;glib.h&gt;
  157. </pre>
  158. </div>
  159. <div class="refsect1">
  160. <a name="glib-Error-Reporting.description"></a><h2>Description</h2>
  161. <p>GLib provides a standard method of reporting errors from a called
  162. function to the calling code. (This is the same problem solved by
  163. exceptions in other languages.) It's important to understand that
  164. this method is both a data type (the <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> struct) and a set of
  165. rules. If you use <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> incorrectly, then your code will not
  166. properly interoperate with other code that uses <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>, and users
  167. of your API will probably get confused.</p>
  168. <p>First and foremost: <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> should only be used to report recoverable
  169. runtime errors, never to report programming errors. If the programmer
  170. 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>, <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>,
  171. <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. (Incidentally,
  172. 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 only be used for
  173. programming errors, it should not be used to print any error
  174. reportable via <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>.)</p>
  175. <p>Examples of recoverable runtime errors are "file not found" or
  176. "failed to parse input." Examples of programming errors are "NULL
  177. passed to <code class="function">strcmp()</code>" or "attempted to free the same pointer twice."
  178. These two kinds of errors are fundamentally different: runtime errors
  179. should be handled or reported to the user, programming errors should
  180. be eliminated by fixing the bug in the program. This is why most
  181. functions in GLib and GTK+ do not use the <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> facility.</p>
  182. <p>Functions that can fail take a return location for a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> as their
  183. last argument. On error, a new <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> instance will be allocated and
  184. returned to the caller via this argument. For example:</p>
  185. <div class="informalexample">
  186. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  187. <tbody>
  188. <tr>
  189. <td class="listing_lines" align="right"><pre>1
  190. 2
  191. 3
  192. 4</pre></td>
  193. <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>
  194. <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>
  195. <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>
  196. <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>
  197. </tr>
  198. </tbody>
  199. </table>
  200. </div>
  201. <p>
  202. 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
  203. point to a location where an error can be placed. For example:</p>
  204. <div class="informalexample">
  205. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  206. <tbody>
  207. <tr>
  208. <td class="listing_lines" align="right"><pre>1
  209. 2
  210. 3
  211. 4
  212. 5
  213. 6
  214. 7
  215. 8
  216. 9
  217. 10
  218. 11
  219. 12
  220. 13
  221. 14
  222. 15
  223. 16
  224. 17</pre></td>
  225. <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>
  226. <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>
  227. <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>
  228. <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>
  229. <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>
  230. <span class="normal"> </span><span class="cbracket">{</span>
  231. <span class="normal"> </span><span class="comment">// Report error to user, and free error</span>
  232. <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>
  233. <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>
  234. <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>
  235. <span class="normal"> </span><span class="cbracket">}</span>
  236. <span class="keyword">else</span>
  237. <span class="normal"> </span><span class="cbracket">{</span>
  238. <span class="normal"> </span><span class="comment">// Use file contents</span>
  239. <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>
  240. <span class="normal"> </span><span class="cbracket">}</span></pre></td>
  241. </tr>
  242. </tbody>
  243. </table>
  244. </div>
  245. <p>
  246. Note that <code class="literal">err != NULL</code> in this example is a reliable indicator
  247. of whether <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,
  248. <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 boolean which
  249. indicates whether it was successful.</p>
  250. <p>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
  251. are only interested in whether it failed and don't need to display
  252. an error message, you can pass <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> for the <em class="parameter"><code>error</code></em>
  253. argument:</p>
  254. <div class="informalexample">
  255. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  256. <tbody>
  257. <tr>
  258. <td class="listing_lines" align="right"><pre>1
  259. 2
  260. 3
  261. 4
  262. 5
  263. 6</pre></td>
  264. <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>
  265. <span class="normal"> </span><span class="comment">// no error occurred </span>
  266. <span class="normal"> </span><span class="symbol">;</span>
  267. <span class="keyword">else</span>
  268. <span class="normal"> </span><span class="comment">// error</span>
  269. <span class="normal"> </span><span class="symbol">;</span></pre></td>
  270. </tr>
  271. </tbody>
  272. </table>
  273. </div>
  274. <p></p>
  275. <p>The <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> object contains three fields: <em class="parameter"><code>domain</code></em>
  276. indicates the module
  277. the error-reporting function is located in, <em class="parameter"><code>code</code></em>
  278. indicates the specific
  279. error that occurred, and <em class="parameter"><code>message</code></em>
  280. is a user-readable error message with
  281. as many details as possible. Several functions are provided to deal
  282. with an error received from a called function: <a class="link" href="glib-Error-Reporting.html#g-error-matches" title="g_error_matches ()"><code class="function">g_error_matches()</code></a>
  283. 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,
  284. <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
  285. calling 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
  286. error location by freeing the error and resetting the location to
  287. <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>. To display an error to the user, simply display the <em class="parameter"><code>message</code></em>
  288. ,
  289. perhaps along with additional context known only to the calling
  290. function (the file being opened, or whatever - though in the
  291. <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, the <em class="parameter"><code>message</code></em>
  292. already contains a filename).</p>
  293. <p>When implementing a function that can report errors, the basic
  294. tool is <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
  295. 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>, 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>
  296. does nothing if the error location passed to it is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a>.
  297. Here's an example:</p>
  298. <div class="informalexample">
  299. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  300. <tbody>
  301. <tr>
  302. <td class="listing_lines" align="right"><pre>1
  303. 2
  304. 3
  305. 4
  306. 5
  307. 6
  308. 7
  309. 8
  310. 9
  311. 10
  312. 11
  313. 12
  314. 13
  315. 14
  316. 15
  317. 16
  318. 17
  319. 18
  320. 19</pre></td>
  321. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gint">gint</a></span>
  322. <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>
  323. <span class="cbracket">{</span>
  324. <span class="normal"> </span><span class="usertype">gint</span><span class="normal"> fd</span><span class="symbol">;</span>
  325. <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>
  326. <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>
  327. <span class="normal"> </span><span class="cbracket">{</span>
  328. <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>
  329. <span class="normal"> FOO_ERROR</span><span class="symbol">,</span><span class="normal"> </span><span class="comment">// error domain</span>
  330. <span class="normal"> FOO_ERROR_BLAH</span><span class="symbol">,</span><span class="normal"> </span><span class="comment">// error code</span>
  331. <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>
  332. <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>
  333. <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>
  334. <span class="normal"> </span><span class="cbracket">}</span>
  335. <span class="normal"> </span><span class="keyword">else</span>
  336. <span class="normal"> </span><span class="keyword">return</span><span class="normal"> fd</span><span class="symbol">;</span>
  337. <span class="cbracket">}</span></pre></td>
  338. </tr>
  339. </tbody>
  340. </table>
  341. </div>
  342. <p></p>
  343. <p>Things are somewhat more complicated if you yourself call another
  344. function that can report a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>. If the sub-function indicates
  345. fatal errors in some way other than reporting a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>, such as
  346. by returning <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> on success, you can simply do the following:</p>
  347. <div class="informalexample">
  348. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  349. <tbody>
  350. <tr>
  351. <td class="listing_lines" align="right"><pre>1
  352. 2
  353. 3
  354. 4
  355. 5
  356. 6
  357. 7
  358. 8
  359. 9
  360. 10
  361. 11
  362. 12
  363. 13
  364. 14
  365. 15</pre></td>
  366. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  367. <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>
  368. <span class="cbracket">{</span>
  369. <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>
  370. <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>
  371. <span class="normal"> </span><span class="cbracket">{</span>
  372. <span class="normal"> </span><span class="comment">// assert that error was set by the sub-function</span>
  373. <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>
  374. <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>
  375. <span class="normal"> </span><span class="cbracket">}</span>
  376. <span class="normal"> </span><span class="comment">// otherwise continue, no error occurred</span>
  377. <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>
  378. <span class="cbracket">}</span></pre></td>
  379. </tr>
  380. </tbody>
  381. </table>
  382. </div>
  383. <p></p>
  384. <p>If the sub-function does not indicate errors other than by
  385. reporting a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> (or if its return value does not reliably indicate
  386. errors) you need to create a temporary <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>
  387. 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>. <a class="link" href="glib-Error-Reporting.html#g-propagate-error" title="g_propagate_error ()"><code class="function">g_propagate_error()</code></a> is
  388. intended for use in this case.</p>
  389. <div class="informalexample">
  390. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  391. <tbody>
  392. <tr>
  393. <td class="listing_lines" align="right"><pre>1
  394. 2
  395. 3
  396. 4
  397. 5
  398. 6
  399. 7
  400. 8
  401. 9
  402. 10
  403. 11
  404. 12
  405. 13
  406. 14
  407. 15
  408. 16
  409. 17
  410. 18
  411. 19
  412. 20</pre></td>
  413. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  414. <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>
  415. <span class="cbracket">{</span>
  416. <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>
  417. <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>
  418. <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>
  419. <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>
  420. <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>
  421. <span class="normal"> </span><span class="cbracket">{</span>
  422. <span class="normal"> </span><span class="comment">// store tmp_error in err, if err != NULL,</span>
  423. <span class="normal"> </span><span class="comment">// otherwise call g_error_free() on tmp_error</span>
  424. <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>
  425. <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>
  426. <span class="normal"> </span><span class="cbracket">}</span>
  427. <span class="normal"> </span><span class="comment">// otherwise continue, no error occurred</span>
  428. <span class="cbracket">}</span></pre></td>
  429. </tr>
  430. </tbody>
  431. </table>
  432. </div>
  433. <p></p>
  434. <p>Error pileups are always a bug. For example, this code is incorrect:</p>
  435. <div class="informalexample">
  436. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  437. <tbody>
  438. <tr>
  439. <td class="listing_lines" align="right"><pre>1
  440. 2
  441. 3
  442. 4
  443. 5
  444. 6
  445. 7
  446. 8
  447. 9
  448. 10
  449. 11
  450. 12
  451. 13
  452. 14
  453. 15
  454. 16
  455. 17</pre></td>
  456. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  457. <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>
  458. <span class="cbracket">{</span>
  459. <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>
  460. <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>
  461. <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>
  462. <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>
  463. <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>
  464. <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>
  465. <span class="normal"> </span><span class="cbracket">{</span>
  466. <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>
  467. <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>
  468. <span class="normal"> </span><span class="cbracket">}</span>
  469. <span class="cbracket">}</span></pre></td>
  470. </tr>
  471. </tbody>
  472. </table>
  473. </div>
  474. <p>
  475. <em class="parameter"><code>tmp_error</code></em>
  476. should be checked immediately after <code class="function">sub_function_that_can_fail()</code>,
  477. and either cleared or propagated upward. The rule is: after each error,
  478. you must either handle the error, or return it to the calling function.</p>
  479. <p>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 equivalent
  480. of handling an error by always doing nothing about it. So the
  481. following code is fine, assuming errors in <code class="function">sub_function_that_can_fail()</code>
  482. are not fatal to <code class="function">my_function_that_can_fail()</code>:</p>
  483. <div class="informalexample">
  484. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  485. <tbody>
  486. <tr>
  487. <td class="listing_lines" align="right"><pre>1
  488. 2
  489. 3
  490. 4
  491. 5
  492. 6
  493. 7
  494. 8
  495. 9
  496. 10
  497. 11
  498. 12
  499. 13
  500. 14
  501. 15
  502. 16
  503. 17
  504. 18</pre></td>
  505. <td class="listing_code"><pre class="programlisting"><span class="normal"><a href="glib-Basic-Types.html#gboolean">gboolean</a></span>
  506. <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>
  507. <span class="cbracket">{</span>
  508. <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>
  509. <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>
  510. <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>
  511. <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>
  512. <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>
  513. <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>
  514. <span class="normal"> </span><span class="cbracket">{</span>
  515. <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>
  516. <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>
  517. <span class="normal"> </span><span class="cbracket">}</span>
  518. <span class="cbracket">}</span></pre></td>
  519. </tr>
  520. </tbody>
  521. </table>
  522. </div>
  523. <p></p>
  524. <p>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 ignores errors;
  525. it's equivalent to
  526. <code class="literal">try { <code class="function">sub_function_that_can_fail()</code>; } catch (...) {}</code>
  527. in C++. It does not mean to leave errors unhandled; it means
  528. to handle them by doing nothing.</p>
  529. <p>Error domains and codes are conventionally named as follows:</p>
  530. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  531. <li class="listitem">
  532. <p>The error domain is called &lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR,
  533. for example <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>:</p>
  534. <div class="informalexample">
  535. <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
  536. <tbody>
  537. <tr>
  538. <td class="listing_lines" align="right"><pre>1
  539. 2
  540. 3
  541. 4
  542. 5
  543. 6
  544. 7</pre></td>
  545. <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>
  546. <span class="normal"><a href="glib-Quarks.html#GQuark">GQuark</a></span>
  547. <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>
  548. <span class="cbracket">{</span>
  549. <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>
  550. <span class="cbracket">}</span></pre></td>
  551. </tr>
  552. </tbody>
  553. </table>
  554. </div>
  555. <p></p>
  556. </li>
  557. <li class="listitem"><p>The quark function for the error domain is called
  558. &lt;namespace&gt;_&lt;module&gt;_error_quark,
  559. for example <code class="function">g_spawn_error_quark()</code> or <code class="function">g_thread_error_quark()</code>.</p></li>
  560. <li class="listitem"><p>The error codes are in an enumeration called
  561. &lt;Namespace&gt;&lt;Module&gt;Error;
  562. for example, <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>.</p></li>
  563. <li class="listitem"><p>Members of the error code enumeration are called
  564. &lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_&lt;CODE&gt;,
  565. 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>.</p></li>
  566. <li class="listitem"><p>If there's a "generic" or "unknown" error code for unrecoverable
  567. errors it doesn't make sense to distinguish with specific codes,
  568. it should be called &lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_FAILED,
  569. for example <a class="link" href="glib-Spawning-Processes.html#G-SPAWN-ERROR-FAILED:CAPS"><code class="literal">G_SPAWN_ERROR_FAILED</code></a>. In the case of error code
  570. enumerations that may be extended in future releases, you should
  571. generally not handle this error code explicitly, but should
  572. instead treat any unrecognized error code as equivalent to
  573. FAILED.</p></li>
  574. </ul></div>
  575. <p>Summary of rules for use of <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>:</p>
  576. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  577. <li class="listitem"><p>Do not report programming errors via <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>.</p></li>
  578. <li class="listitem"><p>The last argument of a function that returns an error should
  579. be a location where a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> can be placed (i.e. "<a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>** error").
  580. If <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> is used with varargs, the <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>** should be the last
  581. argument before the "...".</p></li>
  582. <li class="listitem"><p>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="struct GError"><span class="type">GError</span></a>** if they are not interested
  583. in details of the exact error that occurred.</p></li>
  584. <li class="listitem"><p>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="struct GError"><span class="type">GError</span></a>** argument, then errors should
  585. not be returned to the caller, but your function should still
  586. abort and return if an error occurs. That is, control flow should
  587. not be affected by whether the caller wants to get a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>.</p></li>
  588. <li class="listitem"><p>If a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> is reported, then your function by definition had a
  589. fatal failure and did not complete whatever it was supposed to do.
  590. If the failure was not fatal, then you handled it and you should not
  591. report it. If it was fatal, then you must report it and discontinue
  592. whatever you were doing immediately.</p></li>
  593. <li class="listitem"><p>If a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> is reported, out parameters are not guaranteed to
  594. be set to any defined value.</p></li>
  595. <li class="listitem"><p>A <a class="link" href="glib-Error-Reporting.html#GError" title="struct 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
  596. to a function that can report errors.</p></li>
  597. <li class="listitem"><p>"Piling up" errors is always a bug. That is, if you assign a
  598. new <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> to a <a class="link" href="glib-Error-Reporting.html#GError" title="struct 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
  599. the previous error, it indicates that you should have aborted
  600. the operation instead of continuing. If you were able to continue,
  601. you should have cleared 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>.
  602. <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 if you pile up errors.</p></li>
  603. <li class="listitem"><p>By convention, if you return a boolean value indicating success
  604. 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.
  605. &lt;footnote&gt;&lt;para&gt;Avoid creating functions which have a boolean
  606. return value and a GError parameter, but where the boolean does
  607. something other than signal whether the GError is set. Among other
  608. problems, it requires C callers to allocate a temporary error. Instead,
  609. provide a "gboolean *" out parameter. There are functions in GLib
  610. itself such as <a class="link" href="glib-Key-value-file-parser.html#g-key-file-has-key" title="g_key_file_has_key ()"><code class="function">g_key_file_has_key()</code></a> that are deprecated because of this.
  611. &lt;/para&gt;&lt;/footnote&gt;
  612. If <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
  613. returned, the error must be set to a non-<a class="link" href="glib-Standard-Macros.html#NULL:CAPS" title="NULL"><code class="literal">NULL</code></a> value.
  614. &lt;footnote&gt;&lt;para&gt;One exception to this is that in situations that are
  615. already considered to be undefined behaviour (such as when a
  616. <a class="link" href="glib-Warnings-and-Assertions.html#g-return-val-if-fail" title="g_return_val_if_fail()"><code class="function">g_return_val_if_fail()</code></a> check fails), the error need not be set.
  617. Instead of checking separately whether the error is set, callers
  618. should ensure that they do not provoke undefined behaviour, then
  619. assume that the error will be set on failure.&lt;/para&gt;&lt;/footnote&gt;</p></li>
  620. <li class="listitem"><p>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
  621. 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>
  622. is 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,
  623. then users must check whether an error was returned to see if the
  624. function succeeded.</p></li>
  625. <li class="listitem"><p>When implementing a function that ca

Large files files are truncated, but you can click here to view the full file