2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Tcl error handling</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Tcl API</dl></h3></td>
14
<td align=right><a href="../../ref/tcl/program.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/tcl/faq.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Tcl error handling</h1>
18
<p>The Tcl interfaces to Berkeley DB generally return TCL_OK on success and throw
19
a Tcl error on failure, using the appropriate Tcl interfaces to provide
20
the user with an informative error message. There are some "expected"
21
failures, however, for which no Tcl error will be thrown and for which
22
Tcl commands will return TCL_OK. These failures include times when a
23
searched-for key is not found, a requested key/data pair was previously
24
deleted, or a key/data pair cannot be written because the key already
26
<p>These failures can be detected by searching the Berkeley DB error message that
27
is returned. For example, use the following to detect that an attempt
28
to put a record into the database failed because the key already
30
<p><blockquote><pre>% berkdb open -create -btree a.db
34
% set ret [db0 put -nooverwrite dog newcat]
35
DB_KEYEXIST: Key/data pair already exists
36
% if { [string first DB_KEYEXIST $ret] != -1 } {
37
puts "This was an error; the key existed"
39
This was an error; the key existed
42
% exit</pre></blockquote>
43
<p>To simplify parsing, it is recommended that the initial Berkeley DB error name
44
be checked; for example, <a href="../../api_c/dbc_put.html#DB_KEYEXIST">DB_KEYEXIST</a> in the previous example.
45
To ensure that Tcl scripts are not broken by upgrading to new releases
46
of Berkeley DB, these values will not change in future releases of Berkeley DB.
47
There are currently only three such "expected" error returns:
48
<p><blockquote><pre>DB_NOTFOUND: No matching key/data pair found
49
DB_KEYEMPTY: Nonexistent key/data pair
50
DB_KEYEXIST: Key/data pair already exists</pre></blockquote>
51
<p>Finally, sometimes Berkeley DB will output additional error information when
52
a Berkeley DB error occurs. By default, all Berkeley DB error messages will be
53
prefixed with the created command in whose context the error occurred
54
(for example, "env0", "db2", and so on). There are several ways to
55
capture and access this information.
56
<p>First, if Berkeley DB invokes the error callback function, the additional
57
information will be placed in the error result returned from the command
58
and in the errorInfo backtrace variable in Tcl.
59
<p>Also, the two calls to open an environment and open a database take an
60
option, <b>-errfile filename</b>, which sets an output file to which
61
these additional error messages should be written.
62
<p>Additionally, the two calls to open an environment and open a database
63
take an option, <b>-errpfx string</b>, which sets the error prefix to
64
the given string. This option may be useful in circumstances where a
65
more descriptive prefix is desired or where a constant prefix indicating
67
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/tcl/program.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/tcl/faq.html"><img src="../../images/next.gif" alt="Next"></a>
69
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>