Exception-Handler-Usage.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <html lang="en">
  2. <head>
  3. <title>Exception Handler Usage - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Exception-Handling.html#Exception-Handling" title="Exception Handling">
  9. <link rel="prev" href="Computable-Error-Messages.html#Computable-Error-Messages" title="Computable Error Messages">
  10. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  11. <meta http-equiv="Content-Style-Type" content="text/css">
  12. <style type="text/css"><!--
  13. pre.display { font-family:inherit }
  14. pre.format { font-family:inherit }
  15. pre.smalldisplay { font-family:inherit; font-size:smaller }
  16. pre.smallformat { font-family:inherit; font-size:smaller }
  17. pre.smallexample { font-size:smaller }
  18. pre.smalllisp { font-size:smaller }
  19. span.sc { font-variant:small-caps }
  20. span.roman { font-family:serif; font-weight:normal; }
  21. span.sansserif { font-family:sans-serif; font-weight:normal; }
  22. --></style>
  23. </head>
  24. <body>
  25. <div class="node">
  26. <a name="Exception-Handler-Usage"></a>
  27. <p>
  28. Previous:&nbsp;<a rel="previous" accesskey="p" href="Computable-Error-Messages.html#Computable-Error-Messages">Computable Error Messages</a>,
  29. Up:&nbsp;<a rel="up" accesskey="u" href="Exception-Handling.html#Exception-Handling">Exception Handling</a>
  30. <hr>
  31. </div>
  32. <h5 class="subsubsection">2.7.15.6 Exception Handler Usage</h5>
  33. <p>One way for this feature of the virtual machine to be used is to
  34. intercept and translate error messages to a more meaningful form. An
  35. application guarded as shown below causes messages of invalid deconstruction
  36. to be changed to <code>'syntax error'</code>.
  37. <pre class="display"> <code>main = guard(
  38. application,
  39. conditional(
  40. bu(compare,('invalid deconstruction',nil)),
  41. (constant ('syntax error',nil),identity)))</code>
  42. </pre>
  43. <p class="noindent">The conditional compares its argument to the error message for an
  44. <a name="index-deconstruction-370"></a>invalid deconstruction, and if it matches, the syntax error message is
  45. returned, but otherwise the original message is returned. Note that an
  46. error message must be in the form of a list of character strings, so
  47. that it can be printed. Although the message of <code>'syntax error'</code>
  48. might not be very informative, at least it looks less like a crash.
  49. A real application should of course strive to do better than that.
  50. <p>Exception handling features of the virtual machine can also be adapted
  51. by applications to raise their own exceptions with customized messages.
  52. <pre class="example"> error_messenger =
  53. guard(compose(compare,constant nil),constant ('syntax error',nil))
  54. </pre>
  55. <p class="noindent">This code fragment implements a function that causes a message of
  56. <code>'syntax error'</code> to be reported for any possible input. This code
  57. works by first causing an invalid comparison and then substituting its
  58. own error message. A function that always causes an error is not useful
  59. in itself, but might be used as part of an application in the following
  60. form.
  61. <pre class="example"> main = conditional(validation,(application,error_messenger))
  62. </pre>
  63. <p class="noindent">In this case, the application checks the validity of the input with a
  64. predicate, and invokes the error messenger if it is invalid.
  65. <p>Although the previous examples return a fixed error message for each
  66. possible kind of error, it is also possible to have error messages
  67. that depend on the input data, as the next example shows.
  68. <a name="index-g_t_0040code_007bbu_007d-371"></a><a name="index-g_t_0040code_007bguard_007d-372"></a><a name="index-g_t_0040code_007bidentity_007d-373"></a><a name="index-g_t_0040code_007bapply_007d-374"></a><a name="index-g_t_0040code_007bhired_007d-375"></a>
  69. <pre class="example"> main = (hired apply)(
  70. compose(
  71. bu(guard,some_application),
  72. (hired constant)(constant 'invalid input was:',identity)),
  73. identity)
  74. </pre>
  75. <p class="noindent">If the application causes an exception for any reason, the error message
  76. returned will include a complete listing of the input, prefaced by the
  77. words <code>'invalid input was:'</code>. This particular example works only if
  78. the input is a list of character strings, but could be adapted for other
  79. types of data by substituting an appropriate formatting function for the
  80. first identity. The formatting function would take the relevant data
  81. type to a list of character strings. Another possible variation would be to
  82. concatenate the invalid input listing with the error message that was
  83. generated, rather than just replacing it.
  84. <p>As the last example may suggest, exception handlers turn out to be an
  85. <a name="index-debugging-376"></a><a name="index-functional-programming-377"></a><a name="index-imperative-programming-378"></a>essential debugging tool for functional programs, making them as easy to
  86. debug as imperative programs if not more so. This example forms the
  87. basis for a higher order function that wraps any given function with an
  88. exception handler that prints the argument causing it to crash. For
  89. arguments not causing a crash, the behavior is unchanged. Alternatively,
  90. code implementing a function that unconditionally reports its argument
  91. in an error message can be inserted at a strategic point in the
  92. application code similarly to a print statement. Finally, inspired use
  93. of exception handlers that concatenate their messages with previously
  94. generated messages can show something like a parameter stack dump when a
  95. recursively defined function crashes. These are all matters for a language
  96. designer and are not pursued further in this document.
  97. </body></html>