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>Exception 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="Getting Started with Berkeley DB" /> 10 <link rel="up" href="introduction.html" title="Chapter 1. Introduction to Berkeley DB" /> 11 <link rel="prev" href="environments.html" title="Environments" /> 12 <link rel="next" href="returns.html" title="Error Returns" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Exception Handling</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="environments.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 1. Introduction to Berkeley DB </th> 23 <td width="20%" align="right"> <a accesskey="n" href="returns.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="coreExceptions"></a>Exception Handling</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 Before continuing, it is useful to spend a few moments on exception 38 handling in DB with the 39 <span>C++ API</span>. 40 </p> 41 <p> 42 <span>By default, most</span> 43 44 DB methods throw 45 <code class="classname">DbException</code> 46 47 in the event of a serious error. 48 49 <span> 50 However, be aware that 51 <code class="classname">DbException</code> does not inherit from 52 <code class="classname">std::exception</code> so your <code class="literal">try</code> 53 blocks should catch both types of exceptions. For example: 54 </span> 55 56 </p> 57 <a id="cxx_intro1"></a> 58 <pre class="programlisting">#include <db_cxx.h> 59 ... 60try 61{ 62 // DB and other code goes here 63} 64catch(DbException &e) 65{ 66 // DB error handling goes here 67} 68catch(std::exception &e) 69{ 70 // All other error handling goes here 71} </pre> 72 <p> 73 You can obtain the DB error number for a 74 <code class="classname">DbException</code> 75 76 by using 77 <span> 78 <code class="methodname">DbException::get_errno()</code>. 79 You can also obtain the informational message associated with that error 80 number using <code class="methodname">DbException::what()</code>. 81 </span> 82 83 </p> 84 <p> 85 If for some reason you do not want to manage 86 <code class="classname">DbException</code> objects in your 87 <code class="literal">try</code> blocks, you can configure DB to suppress them 88 by setting <code class="literal">DB_CXX_NO_EXCEPTIONS</code> for your database and 89 environment handles. In this event, you must manage your DB error 90 conditions using the integer value returned by all DB methods. Be 91 aware that this manual assumes that you want to manage your error 92 conditions using <code class="classname">DbException</code> objects. 93 </p> 94 </div> 95 <div class="navfooter"> 96 <hr /> 97 <table width="100%" summary="Navigation footer"> 98 <tr> 99 <td width="40%" align="left"><a accesskey="p" href="environments.html">Prev</a> </td> 100 <td width="20%" align="center"> 101 <a accesskey="u" href="introduction.html">Up</a> 102 </td> 103 <td width="40%" align="right"> <a accesskey="n" href="returns.html">Next</a></td> 104 </tr> 105 <tr> 106 <td width="40%" align="left" valign="top">Environments </td> 107 <td width="20%" align="center"> 108 <a accesskey="h" href="index.html">Home</a> 109 </td> 110 <td width="40%" align="right" valign="top"> Error Returns</td> 111 </tr> 112 </table> 113 </div> 114 </body> 115</html> 116