1<!--$Id: error.so,v 10.22 2003/10/18 19:15:56 bostic Exp $--> 2<!--Copyright (c) 1997,2008 Oracle. All rights reserved.--> 3<!--See the file LICENSE for redistribution information.--> 4<html> 5<head> 6<title>Berkeley DB Reference Guide: Error support</title> 7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> 8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> 9</head> 10<body bgcolor=white> 11<a name="2"><!--meow--></a> 12<table width="100%"><tr valign=top> 13<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></b></td> 14<td align=right><a href="../am_misc/perm.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am_misc/stability.html"><img src="../../images/next.gif" alt="Next"></a> 15</td></tr></table> 16<p align=center><b>Error support</b></p> 17<p>Berkeley DB offers programmatic support for displaying error return values.</p> 18<p>The <a href="../../api_c/env_strerror.html">db_strerror</a> function returns a pointer to the error 19message corresponding to any Berkeley DB error return, similar to the ANSI C 20strerror function, but is able to handle both system error returns and 21Berkeley DB specific return values.</p> 22<p>For example:</p> 23<blockquote><pre>int ret; 24if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0) { 25 fprintf(stderr, "put failed: %s\n", db_strerror(ret)); 26 return (1); 27}</pre></blockquote> 28<p>There are also two additional error methods, <a href="../../api_c/db_err.html">DB->err</a> and 29<a href="../../api_c/db_err.html">DB->errx</a>. These methods work like the ANSI C X3.159-1989 (ANSI C) printf 30function, taking a printf-style format string and argument list, and 31writing a message constructed from the format string and arguments.</p> 32<p>The <a href="../../api_c/db_err.html">DB->err</a> method appends the standard error string to the 33constructed message; the <a href="../../api_c/db_err.html">DB->errx</a> method does not. These methods 34provide simpler ways of displaying Berkeley DB error messages. For example, 35if your application tracks session IDs in a variable called session_id, 36it can include that information in its error messages:</p> 37<p>Error messages can additionally be configured to always include a prefix 38(for example, the program name) using the <a href="../../api_c/db_set_errpfx.html">DB->set_errpfx</a> method.</p> 39<blockquote><pre>#define DATABASE "access.db" 40<p> 41int ret; 42<p> 43(void)dbp->set_errpfx(dbp, program_name); 44<p> 45if ((ret = dbp->open(dbp, 46 NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { 47 dbp->err(dbp, ret, "%s", DATABASE); 48 dbp->errx(dbp, 49 "contact your system administrator: session ID was %d", 50 session_id); 51 return (1); 52}</pre></blockquote> 53<p>For example, if the program were called my_app and the open call returned 54an EACCESS system error, the error messages shown would appear as follows:</p> 55<blockquote><pre>my_app: access.db: Permission denied. 56my_app: contact your system administrator: session ID was 14</pre></blockquote> 57<table width="100%"><tr><td><br></td><td align=right><a href="../am_misc/perm.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am_misc/stability.html"><img src="../../images/next.gif" alt="Next"></a> 58</td></tr></table> 59<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 60</body> 61</html> 62