1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: Exception Handling</title>
18
19<meta name="description" content="Debugging with GDB: Exception Handling">
20<meta name="keywords" content="Debugging with GDB: Exception Handling">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Python-API.html#Python-API" rel="up" title="Python API">
29<link href="Values-From-Inferior.html#Values-From-Inferior" rel="next" title="Values From Inferior">
30<link href="Basic-Python.html#Basic-Python" rel="previous" title="Basic Python">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Exception-Handling"></a>
65<div class="header">
66<p>
67Next: <a href="Values-From-Inferior.html#Values-From-Inferior" accesskey="n" rel="next">Values From Inferior</a>, Previous: <a href="Basic-Python.html#Basic-Python" accesskey="p" rel="previous">Basic Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
68</div>
69<hr>
70<a name="Exception-Handling-1"></a>
71<h4 class="subsubsection">23.2.2.2 Exception Handling</h4>
72<a name="index-python-exceptions"></a>
73<a name="index-exceptions_002c-python"></a>
74
75<p>When executing the <code>python</code> command, Python exceptions
76uncaught within the Python code are translated to calls to
77<small>GDB</small> error-reporting mechanism.  If the command that called
78<code>python</code> does not handle the error, <small>GDB</small> will
79terminate it and print an error message containing the Python
80exception name, the associated value, and the Python call stack
81backtrace at the point where the exception was raised.  Example:
82</p>
83<div class="smallexample">
84<pre class="smallexample">(gdb) python print foo
85Traceback (most recent call last):
86  File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
87NameError: name 'foo' is not defined
88</pre></div>
89
90<p><small>GDB</small> errors that happen in <small>GDB</small> commands invoked by
91Python code are converted to Python exceptions.  The type of the
92Python exception depends on the error.
93</p>
94<dl compact="compact">
95<dt><code>gdb.error</code>
96<a name="index-gdb_002eerror"></a>
97</dt>
98<dd><p>This is the base class for most exceptions generated by <small>GDB</small>.
99It is derived from <code>RuntimeError</code>, for compatibility with earlier
100versions of <small>GDB</small>.
101</p>
102<p>If an error occurring in <small>GDB</small> does not fit into some more
103specific category, then the generated exception will have this type.
104</p>
105</dd>
106<dt><code>gdb.MemoryError</code>
107<a name="index-gdb_002eMemoryError"></a>
108</dt>
109<dd><p>This is a subclass of <code>gdb.error</code> which is thrown when an
110operation tried to access invalid memory in the inferior.
111</p>
112</dd>
113<dt><code>KeyboardInterrupt</code>
114<a name="index-KeyboardInterrupt"></a>
115</dt>
116<dd><p>User interrupt (via <kbd>C-c</kbd> or by typing <kbd>q</kbd> at a pagination
117prompt) is translated to a Python <code>KeyboardInterrupt</code> exception.
118</p></dd>
119</dl>
120
121<p>In all cases, your exception handler will see the <small>GDB</small> error
122message as its value and the Python call stack backtrace at the Python
123statement closest to where the <small>GDB</small> error occured as the
124traceback.
125</p>
126
127<p>When implementing <small>GDB</small> commands in Python via
128<code>gdb.Command</code>, or functions via <code>gdb.Function</code>, it is useful
129to be able to throw an exception that doesn&rsquo;t cause a traceback to be
130printed.  For example, the user may have invoked the command
131incorrectly.  <small>GDB</small> provides a special exception class that can
132be used for this purpose.
133</p>
134<dl compact="compact">
135<dt><code>gdb.GdbError</code>
136<a name="index-gdb_002eGdbError"></a>
137</dt>
138<dd><p>When thrown from a command or function, this exception will cause the
139command or function to fail, but the Python stack will not be
140displayed.  <small>GDB</small> does not throw this exception itself, but
141rather recognizes it when thrown from user Python code.  Example:
142</p>
143<div class="smallexample">
144<pre class="smallexample">(gdb) python
145&gt;class HelloWorld (gdb.Command):
146&gt;  &quot;&quot;&quot;Greet the whole world.&quot;&quot;&quot;
147&gt;  def __init__ (self):
148&gt;    super (HelloWorld, self).__init__ (&quot;hello-world&quot;, gdb.COMMAND_USER)
149&gt;  def invoke (self, args, from_tty):
150&gt;    argv = gdb.string_to_argv (args)
151&gt;    if len (argv) != 0:
152&gt;      raise gdb.GdbError (&quot;hello-world takes no arguments&quot;)
153&gt;    print &quot;Hello, World!&quot;
154&gt;HelloWorld ()
155&gt;end
156(gdb) hello-world 42
157hello-world takes no arguments
158</pre></div>
159</dd>
160</dl>
161
162<hr>
163<div class="header">
164<p>
165Next: <a href="Values-From-Inferior.html#Values-From-Inferior" accesskey="n" rel="next">Values From Inferior</a>, Previous: <a href="Basic-Python.html#Basic-Python" accesskey="p" rel="previous">Basic Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
166</div>
167
168
169
170</body>
171</html>
172