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="env.html" title="Chapter��9.�� The Berkeley DB Environment" /> 11 <link rel="prev" href="env_open.html" title="Opening databases within the environment" /> 12 <link rel="next" href="env_db_config.html" title="DB_CONFIG configuration file" /> 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="env_open.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��9.�� 23 The Berkeley DB Environment 24 </th> 25 <td width="20%" align="right">��<a accesskey="n" href="env_db_config.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="env_error"></a>Error support</h2> 35 </div> 36 </div> 37 </div> 38 <p>Berkeley DB offers programmatic support for displaying error return values. 39The <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. This is similar to the 41ANSI C strerror interface, but can 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 = dbenv->set_cachesize(dbenv, 0, 32 * 1024, 1)) != 0) { 46 fprintf(stderr, "set_cachesize 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/enverr.html" class="olink">DB_ENV->err()</a> and 50<code class="methodname">DB_ENV->errx()</code>. These methods work like the ANSI C printf function, 51taking a printf-style format string and argument list, and writing a 52message constructed from the format string and arguments.</p> 53 <p>The <a href="../api_reference/C/enverr.html" class="olink">DB_ENV->err()</a> function appends the standard error string to the 54constructed message; the <code class="methodname">DB_ENV->errx()</code> function does not.</p> 55 <p>Error messages can be configured always to include a prefix (for 56example, the program name) using the <a href="../api_reference/C/envset_errpfx.html" class="olink">DB_ENV->set_errpfx()</a> method.</p> 57 <p>These functions provide simpler ways of displaying Berkeley DB error messages:</p> 58 <pre class="programlisting">int ret; 59dbenv->set_errpfx(dbenv, program_name); 60if ((ret = dbenv->open(dbenv, home, 61 DB_CREATE | DB_INIT_LOG | DB_INIT_TXN | DB_USE_ENVIRON, 0)) 62 != 0) { 63 dbenv->err(dbenv, ret, "open: %s", home); 64 dbenv->errx(dbenv, 65 "contact your system administrator: session ID was %d", 66 session_id); 67 return (1); 68}</pre> 69 <p>For example, if the program was called "my_app", and it tried to open 70an environment home directory in "/tmp/home" and the open call returned 71a permission error, the error messages shown would look like this:</p> 72 <pre class="programlisting">my_app: open: /tmp/home: Permission denied. 73my_app: contact your system administrator: session ID was 2</pre> 74 </div> 75 <div class="navfooter"> 76 <hr /> 77 <table width="100%" summary="Navigation footer"> 78 <tr> 79 <td width="40%" align="left"><a accesskey="p" href="env_open.html">Prev</a>��</td> 80 <td width="20%" align="center"> 81 <a accesskey="u" href="env.html">Up</a> 82 </td> 83 <td width="40%" align="right">��<a accesskey="n" href="env_db_config.html">Next</a></td> 84 </tr> 85 <tr> 86 <td width="40%" align="left" valign="top">Opening databases within the environment��</td> 87 <td width="20%" align="center"> 88 <a accesskey="h" href="index.html">Home</a> 89 </td> 90 <td width="40%" align="right" valign="top">��DB_CONFIG configuration file</td> 91 </tr> 92 </table> 93 </div> 94 </body> 95</html> 96