| 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>Tcl error handling</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="tcl.html" title="Chapter 21. Berkeley DB Extensions: Tcl" /> |
| 11 | <link rel="prev" href="tcl_program.html" title="Tcl API programming notes" /> |
| 12 | <link rel="next" href="tcl_faq.html" title="Tcl FAQ" /> |
| 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">Tcl error handling</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="tcl_program.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 21. |
| 26 | Berkeley DB Extensions: Tcl |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="tcl_faq.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="tcl_error"></a>Tcl error handling</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>The Tcl interfaces to Berkeley DB generally return TCL_OK on success and throw |
| 42 | a Tcl error on failure, using the appropriate Tcl interfaces to provide |
| 43 | the user with an informative error message. There are some "expected" |
| 44 | failures, however, for which no Tcl error will be thrown and for which |
| 45 | Tcl commands will return TCL_OK. These failures include times when a |
| 46 | searched-for key is not found, a requested key/data pair was previously |
| 47 | deleted, or a key/data pair cannot be written because the key already |
| 48 | exists.</p> |
| 49 | <p>These failures can be detected by searching the Berkeley DB error message that |
| 50 | is returned. For example, use the following to detect that an attempt |
| 51 | to put a record into the database failed because the key already |
| 52 | existed:</p> |
| 53 | <pre class="programlisting">% berkdb open -create -btree a.db |
| 54 | db0 |
| 55 | % db0 put dog cat |
| 56 | 0 |
| 57 | % set ret [db0 put -nooverwrite dog newcat] |
| 58 | DB_KEYEXIST: Key/data pair already exists |
| 59 | % if { [string first DB_KEYEXIST $ret] != -1 } { |
| 60 | puts "This was an error; the key existed" |
| 61 | } |
| 62 | This was an error; the key existed |
| 63 | % db0 close |
| 64 | 0 |
| 65 | % exit</pre> |
| 66 | <p>To simplify parsing, it is recommended that the initial Berkeley DB error name |
| 67 | be checked; for example, <a href="../api_reference/C/dbcput.html#dbcput_DB_KEYEXIST" class="olink">DB_MULTIPLE</a> in the previous example. |
| 68 | To ensure that Tcl scripts are not broken by upgrading to new releases |
| 69 | of Berkeley DB, these values will not change in future releases of Berkeley DB. |
| 70 | There are currently only three such "expected" error returns:</p> |
| 71 | <pre class="programlisting">DB_NOTFOUND: No matching key/data pair found |
| 72 | DB_KEYEMPTY: Nonexistent key/data pair |
| 73 | DB_KEYEXIST: Key/data pair already exists</pre> |
| 74 | <p>Finally, sometimes Berkeley DB will output additional error information when |
| 75 | a Berkeley DB error occurs. By default, all Berkeley DB error messages will be |
| 76 | prefixed with the created command in whose context the error occurred |
| 77 | (for example, "env0", "db2", and so on). There are several ways to |
| 78 | capture and access this information.</p> |
| 79 | <p>First, if Berkeley DB invokes the error callback function, the additional |
| 80 | information will be placed in the error result returned from the command |
| 81 | and in the errorInfo backtrace variable in Tcl.</p> |
| 82 | <p>Also, the two calls to open an environment and open a database take an |
| 83 | option, <span class="bold"><strong>-errfile filename</strong></span>, which sets an output file to which |
| 84 | these additional error messages should be written.</p> |
| 85 | <p>Additionally, the two calls to open an environment and open a database |
| 86 | take an option, <span class="bold"><strong>-errpfx string</strong></span>, which sets the error prefix to |
| 87 | the given string. This option may be useful in circumstances where a |
| 88 | more descriptive prefix is desired or where a constant prefix indicating |
| 89 | an error is desired.</p> |
| 90 | </div> |
| 91 | <div class="navfooter"> |
| 92 | <hr /> |
| 93 | <table width="100%" summary="Navigation footer"> |
| 94 | <tr> |
| 95 | <td width="40%" align="left"><a accesskey="p" href="tcl_program.html">Prev</a> </td> |
| 96 | <td width="20%" align="center"> |
| 97 | <a accesskey="u" href="tcl.html">Up</a> |
| 98 | </td> |
| 99 | <td width="40%" align="right"> <a accesskey="n" href="tcl_faq.html">Next</a></td> |
| 100 | </tr> |
| 101 | <tr> |
| 102 | <td width="40%" align="left" valign="top">Tcl API programming notes </td> |
| 103 | <td width="20%" align="center"> |
| 104 | <a accesskey="h" href="index.html">Home</a> |
| 105 | </td> |
| 106 | <td width="40%" align="right" valign="top"> Tcl FAQ</td> |
| 107 | </tr> |
| 108 | </table> |
| 109 | </div> |
| 110 | </body> |
| 111 | </html> |