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