Exception-Handler-Usage.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on November 8, 2012 by texi2html 1.82
  4. texi2html was written by:
  5. Lionel Cons <[email protected]> (original author)
  6. Karl Berry <[email protected]>
  7. Olaf Bachmann <[email protected]>
  8. and many others.
  9. Maintained by: Many creative people.
  10. Send bugs and suggestions to <[email protected]>
  11. -->
  12. <head>
  13. <title>avram - a virtual machine code interpreter: 2.7.15.6 Exception Handler Usage</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 2.7.15.6 Exception Handler Usage">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 2.7.15.6 Exception Handler Usage">
  16. <meta name="resource-type" content="document">
  17. <meta name="distribution" content="global">
  18. <meta name="Generator" content="texi2html 1.82">
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <style type="text/css">
  21. <!--
  22. a.summary-letter {text-decoration: none}
  23. blockquote.smallquotation {font-size: smaller}
  24. pre.display {font-family: serif}
  25. pre.format {font-family: serif}
  26. pre.menu-comment {font-family: serif}
  27. pre.menu-preformatted {font-family: serif}
  28. pre.smalldisplay {font-family: serif; font-size: smaller}
  29. pre.smallexample {font-size: smaller}
  30. pre.smallformat {font-family: serif; font-size: smaller}
  31. pre.smalllisp {font-size: smaller}
  32. span.roman {font-family:serif; font-weight:normal;}
  33. span.sansserif {font-family:sans-serif; font-weight:normal;}
  34. ul.toc {list-style: none}
  35. -->
  36. </style>
  37. </head>
  38. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  39. <a name="Exception-Handler-Usage"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="Computable-Error-Messages.html#Computable-Error-Messages" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Interfaces-to-External-Code.html#Interfaces-to-External-Code" title="Next section in reading order"> &gt; </a>]</td>
  43. <td valign="middle" align="left"> &nbsp; </td>
  44. <td valign="middle" align="left">[<a href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  45. <td valign="middle" align="left">[<a href="Exception-Handling.html#Exception-Handling" title="Up section"> Up </a>]</td>
  46. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  47. <td valign="middle" align="left"> &nbsp; </td>
  48. <td valign="middle" align="left"> &nbsp; </td>
  49. <td valign="middle" align="left"> &nbsp; </td>
  50. <td valign="middle" align="left"> &nbsp; </td>
  51. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  52. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  53. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  54. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  55. </tr></table>
  56. <hr size="1">
  57. <a name="Exception-Handler-Usage-1"></a>
  58. <h4 class="subsubsection">2.7.15.6 Exception Handler Usage</h4>
  59. <p>One way for this feature of the virtual machine to be used is to
  60. intercept and translate error messages to a more meaningful form. An
  61. application guarded as shown below causes messages of invalid deconstruction
  62. to be changed to <code>'syntax error'</code>.
  63. </p>
  64. <table><tr><td>&nbsp;</td><td><pre class="display"><code>main = guard(
  65. application,
  66. conditional(
  67. bu(compare,('invalid deconstruction',nil)),
  68. (constant ('syntax error',nil),identity)))</code>
  69. </pre></td></tr></table>
  70. <p>The conditional compares its argument to the error message for an
  71. <a name="index-deconstruction-2"></a>
  72. invalid deconstruction, and if it matches, the syntax error message is
  73. returned, but otherwise the original message is returned. Note that an
  74. error message must be in the form of a list of character strings, so
  75. that it can be printed. Although the message of <code>'syntax error'</code>
  76. might not be very informative, at least it looks less like a crash.
  77. A real application should of course strive to do better than that.
  78. </p>
  79. <p>Exception handling features of the virtual machine can also be adapted
  80. by applications to raise their own exceptions with customized messages.
  81. </p>
  82. <table><tr><td>&nbsp;</td><td><pre class="example">error_messenger =
  83. guard(compose(compare,constant nil),constant ('syntax error',nil))
  84. </pre></td></tr></table>
  85. <p>This code fragment implements a function that causes a message of
  86. <code>'syntax error'</code> to be reported for any possible input. This code
  87. works by first causing an invalid comparison and then substituting its
  88. own error message. A function that always causes an error is not useful
  89. in itself, but might be used as part of an application in the following
  90. form.
  91. </p>
  92. <table><tr><td>&nbsp;</td><td><pre class="example">main = conditional(validation,(application,error_messenger))
  93. </pre></td></tr></table>
  94. <p>In this case, the application checks the validity of the input with a
  95. predicate, and invokes the error messenger if it is invalid.
  96. </p>
  97. <p>Although the previous examples return a fixed error message for each
  98. possible kind of error, it is also possible to have error messages
  99. that depend on the input data, as the next example shows.
  100. <a name="index-bu-2"></a>
  101. <a name="index-guard-1"></a>
  102. <a name="index-identity-1"></a>
  103. <a name="index-apply-1"></a>
  104. <a name="index-hired-1"></a>
  105. </p>
  106. <table><tr><td>&nbsp;</td><td><pre class="example">main = (hired apply)(
  107. compose(
  108. bu(guard,some_application),
  109. (hired constant)(constant 'invalid input was:',identity)),
  110. identity)
  111. </pre></td></tr></table>
  112. <p>If the application causes an exception for any reason, the error message
  113. returned will include a complete listing of the input, prefaced by the
  114. words <code>'invalid input was:'</code>. This particular example works only if
  115. the input is a list of character strings, but could be adapted for other
  116. types of data by substituting an appropriate formatting function for the
  117. first identity. The formatting function would take the relevant data
  118. type to a list of character strings. Another possible variation would be to
  119. concatenate the invalid input listing with the error message that was
  120. generated, rather than just replacing it.
  121. </p>
  122. <p>As the last example may suggest, exception handlers turn out to be an
  123. <a name="index-debugging"></a>
  124. <a name="index-functional-programming-4"></a>
  125. <a name="index-imperative-programming-2"></a>
  126. essential debugging tool for functional programs, making them as easy to
  127. debug as imperative programs if not more so. This example forms the
  128. basis for a higher order function that wraps any given function with an
  129. exception handler that prints the argument causing it to crash. For
  130. arguments not causing a crash, the behavior is unchanged. Alternatively,
  131. code implementing a function that unconditionally reports its argument
  132. in an error message can be inserted at a strategic point in the
  133. application code similarly to a print statement. Finally, inspired use
  134. of exception handlers that concatenate their messages with previously
  135. generated messages can show something like a parameter stack dump when a
  136. recursively defined function crashes. These are all matters for a language
  137. designer and are not pursued further in this document.
  138. </p>
  139. <hr size="1">
  140. <table cellpadding="1" cellspacing="1" border="0">
  141. <tr><td valign="middle" align="left">[<a href="Computable-Error-Messages.html#Computable-Error-Messages" title="Previous section in reading order"> &lt; </a>]</td>
  142. <td valign="middle" align="left">[<a href="Interfaces-to-External-Code.html#Interfaces-to-External-Code" title="Next section in reading order"> &gt; </a>]</td>
  143. <td valign="middle" align="left"> &nbsp; </td>
  144. <td valign="middle" align="left">[<a href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  145. <td valign="middle" align="left">[<a href="Exception-Handling.html#Exception-Handling" title="Up section"> Up </a>]</td>
  146. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  147. <td valign="middle" align="left"> &nbsp; </td>
  148. <td valign="middle" align="left"> &nbsp; </td>
  149. <td valign="middle" align="left"> &nbsp; </td>
  150. <td valign="middle" align="left"> &nbsp; </td>
  151. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  152. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  153. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  154. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  155. </tr></table>
  156. <p>
  157. <font size="-1">
  158. This document was generated on <i>November 8, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  159. </font>
  160. <br>
  161. </p>
  162. </body>
  163. </html>