You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Exception-Handling.html 7.2KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Exception Handling (Debugging with GDB)</title>
  17. <meta name="description" content="Exception Handling (Debugging with GDB)">
  18. <meta name="keywords" content="Exception Handling (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="prev" title="Basic Python">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Exception-Handling"></a>
  58. <div class="header">
  59. <p>
  60. 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="prev">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>
  61. </div>
  62. <hr>
  63. <a name="Exception-Handling-1"></a>
  64. <h4 class="subsubsection">23.2.2.2 Exception Handling</h4>
  65. <a name="index-python-exceptions"></a>
  66. <a name="index-exceptions_002c-python"></a>
  67. <p>When executing the <code>python</code> command, Python exceptions
  68. uncaught within the Python code are translated to calls to
  69. <small>GDB</small> error-reporting mechanism. If the command that called
  70. <code>python</code> does not handle the error, <small>GDB</small> will
  71. terminate it and print an error message containing the Python
  72. exception name, the associated value, and the Python call stack
  73. backtrace at the point where the exception was raised. Example:
  74. </p>
  75. <div class="smallexample">
  76. <pre class="smallexample">(gdb) python print foo
  77. Traceback (most recent call last):
  78. File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
  79. NameError: name 'foo' is not defined
  80. </pre></div>
  81. <p><small>GDB</small> errors that happen in <small>GDB</small> commands invoked by
  82. Python code are converted to Python exceptions. The type of the
  83. Python exception depends on the error.
  84. </p>
  85. <dl compact="compact">
  86. <dt><code>gdb.error</code>
  87. <a name="index-gdb_002eerror"></a>
  88. </dt>
  89. <dd><p>This is the base class for most exceptions generated by <small>GDB</small>.
  90. It is derived from <code>RuntimeError</code>, for compatibility with earlier
  91. versions of <small>GDB</small>.
  92. </p>
  93. <p>If an error occurring in <small>GDB</small> does not fit into some more
  94. specific category, then the generated exception will have this type.
  95. </p>
  96. </dd>
  97. <dt><code>gdb.MemoryError</code>
  98. <a name="index-gdb_002eMemoryError"></a>
  99. </dt>
  100. <dd><p>This is a subclass of <code>gdb.error</code> which is thrown when an
  101. operation tried to access invalid memory in the inferior.
  102. </p>
  103. </dd>
  104. <dt><code>KeyboardInterrupt</code>
  105. <a name="index-KeyboardInterrupt"></a>
  106. </dt>
  107. <dd><p>User interrupt (via <kbd>C-c</kbd> or by typing <kbd>q</kbd> at a pagination
  108. prompt) is translated to a Python <code>KeyboardInterrupt</code> exception.
  109. </p></dd>
  110. </dl>
  111. <p>In all cases, your exception handler will see the <small>GDB</small> error
  112. message as its value and the Python call stack backtrace at the Python
  113. statement closest to where the <small>GDB</small> error occured as the
  114. traceback.
  115. </p>
  116. <p>When implementing <small>GDB</small> commands in Python via
  117. <code>gdb.Command</code>, or functions via <code>gdb.Function</code>, it is useful
  118. to be able to throw an exception that doesn&rsquo;t cause a traceback to be
  119. printed. For example, the user may have invoked the command
  120. incorrectly. <small>GDB</small> provides a special exception class that can
  121. be used for this purpose.
  122. </p>
  123. <dl compact="compact">
  124. <dt><code>gdb.GdbError</code>
  125. <a name="index-gdb_002eGdbError"></a>
  126. </dt>
  127. <dd><p>When thrown from a command or function, this exception will cause the
  128. command or function to fail, but the Python stack will not be
  129. displayed. <small>GDB</small> does not throw this exception itself, but
  130. rather recognizes it when thrown from user Python code. Example:
  131. </p>
  132. <div class="smallexample">
  133. <pre class="smallexample">(gdb) python
  134. &gt;class HelloWorld (gdb.Command):
  135. &gt; &quot;&quot;&quot;Greet the whole world.&quot;&quot;&quot;
  136. &gt; def __init__ (self):
  137. &gt; super (HelloWorld, self).__init__ (&quot;hello-world&quot;, gdb.COMMAND_USER)
  138. &gt; def invoke (self, args, from_tty):
  139. &gt; argv = gdb.string_to_argv (args)
  140. &gt; if len (argv) != 0:
  141. &gt; raise gdb.GdbError (&quot;hello-world takes no arguments&quot;)
  142. &gt; print &quot;Hello, World!&quot;
  143. &gt;HelloWorld ()
  144. &gt;end
  145. (gdb) hello-world 42
  146. hello-world takes no arguments
  147. </pre></div>
  148. </dd>
  149. </dl>
  150. <hr>
  151. <div class="header">
  152. <p>
  153. 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="prev">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>
  154. </div>
  155. </body>
  156. </html>