Exception-Handling.html 7.3 KB

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