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