1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Error support</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> 10 <link rel="up" href="am_misc.html" title="Chapter��4.�� Access Method Wrapup" /> 11 <link rel="prev" href="am_misc_perm.html" title="Retrieved key/data permanence for C/C++" /> 12 <link rel="next" href="am_misc_stability.html" title="Cursor stability" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Error support</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="am_misc_perm.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��4.�� 23 Access Method Wrapup 24 </th> 25 <td width="20%" align="right">��<a accesskey="n" href="am_misc_stability.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="am_misc_error"></a>Error support</h2> 35 </div> 36 </div> 37 </div> 38 <p>Berkeley DB offers programmatic support for displaying error return values.</p> 39 <p>The <a href="../api_reference/C/envstrerror.html" class="olink">db_strerror()</a> function returns a pointer to the error 40message corresponding to any Berkeley DB error return, similar to the ANSI C 41strerror function, but is able to handle both system error returns and 42Berkeley DB specific return values.</p> 43 <p>For example:</p> 44 <pre class="programlisting">int ret; 45if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0) { 46 fprintf(stderr, "put failed: %s\n", db_strerror(ret)); 47 return (1); 48}</pre> 49 <p>There are also two additional error methods, <a href="../api_reference/C/dberr.html" class="olink">DB->err()</a> and 50<code class="methodname">DB->errx()</code>. These methods work like the ANSI C X3.159-1989 (ANSI C) printf 51function, taking a printf-style format string and argument list, and 52writing a message constructed from the format string and arguments.</p> 53 <p>The <a href="../api_reference/C/dberr.html" class="olink">DB->err()</a> method appends the standard error string to the 54constructed message; the <code class="methodname">DB->errx()</code> method does not. These methods 55provide simpler ways of displaying Berkeley DB error messages. For example, 56if your application tracks session IDs in a variable called session_id, 57it can include that information in its error messages:</p> 58 <p>Error messages can additionally be configured to always include a prefix 59(for example, the program name) using the <a href="../api_reference/C/dbset_errpfx.html" class="olink">DB->set_errpfx()</a> method.</p> 60 <pre class="programlisting">#define DATABASE "access.db" 61 62int ret; 63 64(void)dbp->set_errpfx(dbp, program_name); 65 66if ((ret = dbp->open(dbp, 67 NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { 68 dbp->err(dbp, ret, "%s", DATABASE); 69 dbp->errx(dbp, 70 "contact your system administrator: session ID was %d", 71 session_id); 72 return (1); 73}</pre> 74 <p>For example, if the program were called my_app and the open call returned 75an EACCESS system error, the error messages shown would appear as follows:</p> 76 <pre class="programlisting">my_app: access.db: Permission denied. 77my_app: contact your system administrator: session ID was 14</pre> 78 </div> 79 <div class="navfooter"> 80 <hr /> 81 <table width="100%" summary="Navigation footer"> 82 <tr> 83 <td width="40%" align="left"><a accesskey="p" href="am_misc_perm.html">Prev</a>��</td> 84 <td width="20%" align="center"> 85 <a accesskey="u" href="am_misc.html">Up</a> 86 </td> 87 <td width="40%" align="right">��<a accesskey="n" href="am_misc_stability.html">Next</a></td> 88 </tr> 89 <tr> 90 <td width="40%" align="left" valign="top">Retrieved key/data permanence for C/C++��</td> 91 <td width="20%" align="center"> 92 <a accesskey="h" href="index.html">Home</a> 93 </td> 94 <td width="40%" align="right" valign="top">��Cursor stability</td> 95 </tr> 96 </table> 97 </div> 98 </body> 99</html> 100