| 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 xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Error support</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="env_open.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 9. |
| 26 | The Berkeley DB Environment |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="env_db_config.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="env_error"></a>Error support</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>Berkeley DB offers programmatic support for displaying error return values. |
| 42 | The <a href="../api_reference/C/envstrerror.html" class="olink">db_strerror()</a> function returns a pointer to the error |
| 43 | message corresponding to any Berkeley DB error return. This is similar to the |
| 44 | ANSI C strerror interface, but can handle both system error returns and |
| 45 | Berkeley DB-specific return values.</p> |
| 46 | <p>For example:</p> |
| 47 | <a id="prog_env29"></a> |
| 48 | <pre class="programlisting">int ret; |
| 49 | if ((ret = dbenv->set_cachesize(dbenv, 0, 32 * 1024, 1)) != 0) { |
| 50 | fprintf(stderr, "set_cachesize failed: %s\n", db_strerror(ret)); |
| 51 | return (1); |
| 52 | } |
| 53 | </pre> |
| 54 | <p>There are also two additional error methods: <a href="../api_reference/C/enverr.html" class="olink">DB_ENV->err()</a> and |
| 55 | <code class="methodname">DB_ENV->errx()</code>. These methods work like the ANSI C printf function, |
| 56 | taking a printf-style format string and argument list, and writing a |
| 57 | message constructed from the format string and arguments.</p> |
| 58 | <p>The <a href="../api_reference/C/enverr.html" class="olink">DB_ENV->err()</a> function appends the standard error string to the |
| 59 | constructed message; the <code class="methodname">DB_ENV->errx()</code> function does not.</p> |
| 60 | <p>Error messages can be configured always to include a prefix (for |
| 61 | example, the program name) using the <a href="../api_reference/C/envset_errpfx.html" class="olink">DB_ENV->set_errpfx()</a> method.</p> |
| 62 | <p>These functions provide simpler ways of displaying Berkeley DB error messages:</p> |
| 63 | <a id="prog_env30"></a> |
| 64 | <pre class="programlisting">int ret; |
| 65 | ... |
| 66 | dbenv->set_errpfx(dbenv, program_name); |
| 67 | if ((ret = dbenv->open(dbenv, home, |
| 68 | DB_CREATE | DB_INIT_LOG | DB_INIT_TXN | DB_USE_ENVIRON, 0)) |
| 69 | != 0) { |
| 70 | dbenv->err(dbenv, ret, "open: %s", home); |
| 71 | dbenv->errx(dbenv, |
| 72 | "contact your system administrator: session ID was %d", |
| 73 | session_id); |
| 74 | return (1); |
| 75 | }</pre> |
| 76 | <p>For example, if the program was called "my_app", and it tried to open |
| 77 | an environment home directory in "/tmp/home" and the open call returned |
| 78 | a permission error, the error messages shown would look like this:</p> |
| 79 | <pre class="programlisting">my_app: open: /tmp/home: Permission denied. |
| 80 | my_app: contact your system administrator: session ID was 2</pre> |
| 81 | </div> |
| 82 | <div class="navfooter"> |
| 83 | <hr /> |
| 84 | <table width="100%" summary="Navigation footer"> |
| 85 | <tr> |
| 86 | <td width="40%" align="left"><a accesskey="p" href="env_open.html">Prev</a> </td> |
| 87 | <td width="20%" align="center"> |
| 88 | <a accesskey="u" href="env.html">Up</a> |
| 89 | </td> |
| 90 | <td width="40%" align="right"> <a accesskey="n" href="env_db_config.html">Next</a></td> |
| 91 | </tr> |
| 92 | <tr> |
| 93 | <td width="40%" align="left" valign="top">Opening databases within the environment </td> |
| 94 | <td width="20%" align="center"> |
| 95 | <a accesskey="h" href="index.html">Home</a> |
| 96 | </td> |
| 97 | <td width="40%" align="right" valign="top"> DB_CONFIG configuration file</td> |
| 98 | </tr> |
| 99 | </table> |
| 100 | </div> |
| 101 | </body> |
| 102 | </html> |