OSDN Git Service

2009-04-15 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / html / manual / verbose_termination.html
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"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Verbose Terminate Handler</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="termination.html" title="Chapter 6. Termination" /><link rel="prev" href="termination.html" title="Chapter 6. Termination" /><link rel="next" href="diagnostics.html" title="Part III.  Diagnostics" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Verbose Terminate Handler</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="termination.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Termination</th><td width="20%" align="right"> <a accesskey="n" href="diagnostics.html">Next</a></td></tr></table><hr /></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="support.termination.verbose"></a>Verbose Terminate Handler</h2></div></div></div><p>
4       If you are having difficulty with uncaught exceptions and want a
5       little bit of help debugging the causes of the core dumps, you can
6       make use of a GNU extension, the verbose terminate handler.
7     </p><pre class="programlisting">
8 #include &lt;exception&gt;
9   
10 int main()
11 {
12   std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
13   ...
14
15   throw <em class="replaceable"><code>anything</code></em>;
16 }
17 </pre><p>
18      The <code class="function">__verbose_terminate_handler</code> function
19      obtains the name of the current exception, attempts to demangle
20      it, and prints it to stderr.  If the exception is derived from
21      <code class="classname">exception</code> then the output from
22      <code class="function">what()</code> will be included.
23    </p><p>
24      Any replacement termination function is required to kill the
25      program without returning; this one calls abort.
26    </p><p>
27      For example:
28    </p><pre class="programlisting">
29 #include &lt;exception&gt;
30 #include &lt;stdexcept&gt;
31
32 struct argument_error : public std::runtime_error
33 {  
34   argument_error(const std::string&amp; s): std::runtime_error(s) { }
35 };
36
37 int main(int argc)
38 {
39   std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
40   if (argc &gt; 5)
41     throw argument_error(“<span class="quote">argc is greater than 5!</span>”);
42   else
43     throw argc;
44 }
45 </pre><p>
46      With the verbose terminate handler active, this gives:
47    </p><pre class="screen">
48    <code class="computeroutput">
49    % ./a.out
50    terminate called after throwing a `int'
51    Aborted
52    % ./a.out f f f f f f f f f f f
53    terminate called after throwing an instance of `argument_error'
54    what(): argc is greater than 5!
55    Aborted
56    </code>
57    </pre><p>
58      The 'Aborted' line comes from the call to
59      <code class="function">abort()</code>, of course.
60    </p><p>
61      This is the default termination handler; nothing need be done to
62      use it.  To go back to the previous “<span class="quote">silent death</span>”
63      method, simply include <code class="filename">exception</code> and
64      <code class="filename">cstdlib</code>, and call
65    </p><pre class="programlisting">
66      std::set_terminate(std::abort);
67    </pre><p> 
68      After this, all calls to <code class="function">terminate</code> will use
69      <code class="function">abort</code> as the terminate handler.
70    </p><p>
71      Note: the verbose terminate handler will attempt to write to
72      stderr.  If your application closes stderr or redirects it to an
73      inappropriate location,
74      <code class="function">__verbose_terminate_handler</code> will behave in
75      an unspecified manner.
76    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="termination.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="termination.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="diagnostics.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. Termination </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Part III. 
77   Diagnostics
78   
79 </td></tr></table></div></body></html>