1<html lang="en"> 2<head> 3<title>Exception Handling - Debugging with GDB</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Debugging with GDB"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Python-API.html#Python-API" title="Python API"> 9<link rel="prev" href="Basic-Python.html#Basic-Python" title="Basic Python"> 10<link rel="next" href="Values-From-Inferior.html#Values-From-Inferior" title="Values From Inferior"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Free Software'' and ``Free Software Needs 19Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' 20and with the Back-Cover Texts as in (a) below. 21 22(a) The FSF's Back-Cover Text is: ``You are free to copy and modify 23this GNU Manual. Buying copies from GNU Press supports the FSF in 24developing GNU and promoting software freedom.'' 25--> 26<meta http-equiv="Content-Style-Type" content="text/css"> 27<style type="text/css"><!-- 28 pre.display { font-family:inherit } 29 pre.format { font-family:inherit } 30 pre.smalldisplay { font-family:inherit; font-size:smaller } 31 pre.smallformat { font-family:inherit; font-size:smaller } 32 pre.smallexample { font-size:smaller } 33 pre.smalllisp { font-size:smaller } 34 span.sc { font-variant:small-caps } 35 span.roman { font-family:serif; font-weight:normal; } 36 span.sansserif { font-family:sans-serif; font-weight:normal; } 37--></style> 38<link rel="stylesheet" type="text/css" href="../cs.css"> 39</head> 40<body> 41<div class="node"> 42<a name="Exception-Handling"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="Values-From-Inferior.html#Values-From-Inferior">Values From Inferior</a>, 45Previous: <a rel="previous" accesskey="p" href="Basic-Python.html#Basic-Python">Basic Python</a>, 46Up: <a rel="up" accesskey="u" href="Python-API.html#Python-API">Python API</a> 47<hr> 48</div> 49 50<h5 class="subsubsection">23.2.2.2 Exception Handling</h5> 51 52<p><a name="index-python-exceptions-1775"></a><a name="index-exceptions_002c-python-1776"></a> 53When executing the <code>python</code> command, Python exceptions 54uncaught within the Python code are translated to calls to 55<span class="sc">gdb</span> error-reporting mechanism. If the command that called 56<code>python</code> does not handle the error, <span class="sc">gdb</span> will 57terminate it and print an error message containing the Python 58exception name, the associated value, and the Python call stack 59backtrace at the point where the exception was raised. Example: 60 61<pre class="smallexample"> (gdb) python print foo 62 Traceback (most recent call last): 63 File "<string>", line 1, in <module> 64 NameError: name 'foo' is not defined 65</pre> 66 <p><span class="sc">gdb</span> errors that happen in <span class="sc">gdb</span> commands invoked by 67Python code are converted to Python exceptions. The type of the 68Python exception depends on the error. 69 70 <dl> 71<dt><code>gdb.error</code><a name="index-gdb_002eerror-1777"></a><dd>This is the base class for most exceptions generated by <span class="sc">gdb</span>. 72It is derived from <code>RuntimeError</code>, for compatibility with earlier 73versions of <span class="sc">gdb</span>. 74 75 <p>If an error occurring in <span class="sc">gdb</span> does not fit into some more 76specific category, then the generated exception will have this type. 77 78 <br><dt><code>gdb.MemoryError</code><a name="index-gdb_002eMemoryError-1778"></a><dd>This is a subclass of <code>gdb.error</code> which is thrown when an 79operation tried to access invalid memory in the inferior. 80 81 <br><dt><code>KeyboardInterrupt</code><a name="index-KeyboardInterrupt-1779"></a><dd>User interrupt (via <kbd>C-c</kbd> or by typing <kbd>q</kbd> at a pagination 82prompt) is translated to a Python <code>KeyboardInterrupt</code> exception. 83</dl> 84 85 <p>In all cases, your exception handler will see the <span class="sc">gdb</span> error 86message as its value and the Python call stack backtrace at the Python 87statement closest to where the <span class="sc">gdb</span> error occured as the 88traceback. 89 90 <p><a name="index-gdb_002eGdbError-1780"></a>When implementing <span class="sc">gdb</span> commands in Python via <code>gdb.Command</code>, 91it is useful to be able to throw an exception that doesn't cause a 92traceback to be printed. For example, the user may have invoked the 93command incorrectly. Use the <code>gdb.GdbError</code> exception 94to handle this case. Example: 95 96<pre class="smallexample"> (gdb) python 97 >class HelloWorld (gdb.Command): 98 > """Greet the whole world.""" 99 > def __init__ (self): 100 > super (HelloWorld, self).__init__ ("hello-world", gdb.COMMAND_USER) 101 > def invoke (self, args, from_tty): 102 > argv = gdb.string_to_argv (args) 103 > if len (argv) != 0: 104 > raise gdb.GdbError ("hello-world takes no arguments") 105 > print "Hello, World!" 106 >HelloWorld () 107 >end 108 (gdb) hello-world 42 109 hello-world takes no arguments 110</pre> 111 </body></html> 112 113