• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Exception Translation</title>
5<link rel="stylesheet" href="../../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Boost.Python Tutorial">
8<link rel="up" href="../index.html" title="Boost.Python Tutorial">
9<link rel="prev" href="iterators.html" title="Iterators">
10<link rel="next" href="techniques.html" title="General Techniques">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
14<hr>
15<div class="spirit-nav">
16<a accesskey="p" href="iterators.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="techniques.html"><img src="../../images/next.png" alt="Next"></a>
17</div>
18<div class="section">
19<div class="titlepage"><div><div><h2 class="title" style="clear: both">
20<a name="tutorial.exception"></a><a class="link" href="exception.html" title="Exception Translation">Exception Translation</a>
21</h2></div></div></div>
22<p>
23      All C++ exceptions must be caught at the boundary with Python code. This boundary
24      is the point where C++ meets Python. Boost.Python provides a default exception
25      handler that translates selected standard exceptions, then gives up:
26    </p>
27<pre class="programlisting"><span class="keyword">raise</span> <span class="identifier">RuntimeError</span><span class="special">,</span> <span class="string">'unidentifiable C++ Exception'</span>
28</pre>
29<p>
30      Users may provide custom translation. Here's an example:
31    </p>
32<pre class="programlisting"><span class="identifier">struct</span> <span class="identifier">PodBayDoorException</span><span class="special">;</span>
33<span class="identifier">void</span> <span class="identifier">translator</span><span class="special">(</span><span class="identifier">PodBayDoorException</span> <span class="identifier">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
34    <span class="identifier">PyErr_SetString</span><span class="special">(</span><span class="identifier">PyExc_UserWarning</span><span class="special">,</span> <span class="string">"I'm sorry Dave..."</span><span class="special">);</span>
35<span class="special">}</span>
36<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">kubrick</span><span class="special">)</span> <span class="special">{</span>
37     <span class="identifier">register_exception_translator</span><span class="special">&lt;</span>
38          <span class="identifier">PodBayDoorException</span><span class="special">&gt;(</span><span class="identifier">translator</span><span class="special">);</span>
39     <span class="special">...</span>
40</pre>
41</div>
42<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
43<td align="left"></td>
44<td align="right"><div class="copyright-footer">Copyright © 2002-2005 Joel
45      de Guzman, David Abrahams<p>
46        Distributed under the Boost Software License, Version 1.0. (See accompanying
47        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
48      </p>
49</div></td>
50</tr></table>
51<hr>
52<div class="spirit-nav">
53<a accesskey="p" href="iterators.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="techniques.html"><img src="../../images/next.png" alt="Next"></a>
54</div>
55</body>
56</html>
57