1<!--$Id: error.so,v 10.20 2003/10/18 19:16:00 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<table width="100%"><tr valign=top> 12<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Environment</dl></b></td> 13<td align=right><a href="/env/open.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/env/db_config.html"><img src="/images/next.gif" alt="Next"></a> 14</td></tr></table> 15<p align=center><b>Error support</b></p> 16<p>Berkeley DB offers programmatic support for displaying error return values. 17The <a href="/api_c/env_strerror.html">db_strerror</a> function returns a pointer to the error 18message corresponding to any Berkeley DB error return. This is similar to the 19ANSI C strerror interface, but can handle both system error returns and 20Berkeley DB-specific return values.</p> 21<p>For example:</p> 22<blockquote><pre>int ret; 23if ((ret = dbenv->set_cachesize(dbenv, 0, 32 * 1024, 1)) != 0) { 24 fprintf(stderr, "set_cachesize failed: %s\n", db_strerror(ret)); 25 return (1); 26}</pre></blockquote> 27<p>There are also two additional error methods: <a href="/api_c/env_err.html">DB_ENV->err</a> and 28<a href="/api_c/env_err.html">DB_ENV->errx</a>. These methods work like the ANSI C printf function, 29taking a printf-style format string and argument list, and writing a 30message constructed from the format string and arguments.</p> 31<p>The <a href="/api_c/env_err.html">DB_ENV->err</a> function appends the standard error string to the 32constructed message; the <a href="/api_c/env_err.html">DB_ENV->errx</a> function does not.</p> 33<p>Error messages can be configured always to include a prefix (for 34example, the program name) using the <a href="/api_c/env_set_errpfx.html">DB_ENV->set_errpfx</a> method.</p> 35<p>These functions provide simpler ways of displaying Berkeley DB error messages:</p> 36<blockquote><pre>int ret; 37dbenv->set_errpfx(dbenv, program_name); 38if ((ret = dbenv->open(dbenv, home, 39 DB_CREATE | DB_INIT_LOG | DB_INIT_TXN | DB_USE_ENVIRON, 0)) 40 != 0) { 41 dbenv->err(dbenv, ret, "open: %s", home); 42 dbenv->errx(dbenv, 43 "contact your system administrator: session ID was %d", 44 session_id); 45 return (1); 46}</pre></blockquote> 47<p>For example, if the program was called "my_app", and it tried to open 48an environment home directory in "/tmp/home" and the open call returned 49a permission error, the error messages shown would look like this:</p> 50<blockquote><pre>my_app: open: /tmp/home: Permission denied. 51my_app: contact your system administrator: session ID was 2</pre></blockquote> 52<table width="100%"><tr><td><br></td><td align=right><a href="/env/open.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/env/db_config.html"><img src="/images/next.gif" alt="Next"></a> 53</td></tr></table> 54<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 55</body> 56</html> 57