gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

60 行
5.8KB

  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"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>43.8. Error Handling in PL/Tcl</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="pltcl-event-trigger.html" title="43.7. Event Trigger Functions in PL/Tcl" /><link rel="next" href="pltcl-subtransactions.html" title="43.9. Explicit Subtransactions in PL/Tcl" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">43.8. Error Handling in PL/Tcl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pltcl-event-trigger.html" title="43.7. Event Trigger Functions in PL/Tcl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="pltcl.html" title="Chapter 43. PL/Tcl - Tcl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 43. PL/Tcl - Tcl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pltcl-subtransactions.html" title="43.9. Explicit Subtransactions in PL/Tcl">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLTCL-ERROR-HANDLING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">43.8. Error Handling in PL/Tcl</h2></div></div></div><a id="id-1.8.9.12.2" class="indexterm"></a><p>
  3. Tcl code within or called from a PL/Tcl function can raise an error,
  4. either by executing some invalid operation or by generating an error
  5. using the Tcl <code class="function">error</code> command or
  6. PL/Tcl's <code class="function">elog</code> command. Such errors can be caught
  7. within Tcl using the Tcl <code class="function">catch</code> command. If an
  8. error is not caught but is allowed to propagate out to the top level of
  9. execution of the PL/Tcl function, it is reported as a SQL error in the
  10. function's calling query.
  11. </p><p>
  12. Conversely, SQL errors that occur within PL/Tcl's
  13. <code class="function">spi_exec</code>, <code class="function">spi_prepare</code>,
  14. and <code class="function">spi_execp</code> commands are reported as Tcl errors,
  15. so they are catchable by Tcl's <code class="function">catch</code> command.
  16. (Each of these PL/Tcl commands runs its SQL operation in a
  17. subtransaction, which is rolled back on error, so that any
  18. partially-completed operation is automatically cleaned up.)
  19. Again, if an error propagates out to the top level without being caught,
  20. it turns back into a SQL error.
  21. </p><p>
  22. Tcl provides an <code class="varname">errorCode</code> variable that can represent
  23. additional information about an error in a form that is easy for Tcl
  24. programs to interpret. The contents are in Tcl list format, and the
  25. first word identifies the subsystem or library reporting the error;
  26. beyond that the contents are left to the individual subsystem or
  27. library. For database errors reported by PL/Tcl commands, the first
  28. word is <code class="literal">POSTGRES</code>, the second word is the PostgreSQL
  29. version number, and additional words are field name/value pairs
  30. providing detailed information about the error.
  31. Fields <code class="varname">SQLSTATE</code>, <code class="varname">condition</code>,
  32. and <code class="varname">message</code> are always supplied
  33. (the first two represent the error code and condition name as shown
  34. in <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>).
  35. Fields that may be present include
  36. <code class="varname">detail</code>, <code class="varname">hint</code>, <code class="varname">context</code>,
  37. <code class="varname">schema</code>, <code class="varname">table</code>, <code class="varname">column</code>,
  38. <code class="varname">datatype</code>, <code class="varname">constraint</code>,
  39. <code class="varname">statement</code>, <code class="varname">cursor_position</code>,
  40. <code class="varname">filename</code>, <code class="varname">lineno</code>, and
  41. <code class="varname">funcname</code>.
  42. </p><p>
  43. A convenient way to work with PL/Tcl's <code class="varname">errorCode</code>
  44. information is to load it into an array, so that the field names become
  45. array subscripts. Code for doing that might look like
  46. </p><pre class="programlisting">
  47. if {[catch { spi_exec $sql_command }]} {
  48. if {[lindex $::errorCode 0] == "POSTGRES"} {
  49. array set errorArray $::errorCode
  50. if {$errorArray(condition) == "undefined_table"} {
  51. # deal with missing table
  52. } else {
  53. # deal with some other type of SQL error
  54. }
  55. }
  56. }
  57. </pre><p>
  58. (The double colons explicitly specify that <code class="varname">errorCode</code>
  59. is a global variable.)
  60. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pltcl-event-trigger.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="pltcl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pltcl-subtransactions.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.7. Event Trigger Functions in PL/Tcl </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 43.9. Explicit Subtransactions in PL/Tcl</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1