home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson10.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  7.4 KB  |  183 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic & other WWW browsers.>
  4.  
  5. <COMMENT  Edit the following lines. >
  6. <TUTOR NAME="Lovelace">
  7. <LESSON NUMBER=10>
  8. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  9. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  10. <COMMENT $Id: lesson10.les,v 1.2 1995/05/17 21:25:18 wheeler Exp $ >
  11.  
  12. <COMMENT  You'll probably want to uncomment and edit these lines: >
  13. <COMMENT  <PREVIOUS_LESSON LOCATION="URL_of_directory/" >
  14. <COMMENT  <NEXT_LESSON LOCATION="URL_of_directory/" >
  15.  
  16. <COMMENT A lesson is divided into 1 or more "sections".>
  17. <COMMENT Each section has a title; SECTION starts a new section.>
  18.  
  19. <SECTION NAME="Exception Basics">
  20. Errors and other exceptional situations must be handled by programs
  21. that work in the real world.
  22. Ada provides facilities to deal with these real problems which make
  23. handling them much easier.
  24. In Ada, an <EM>exception</EM> represents a kind of exceptional situation,
  25. usually a serious error.
  26. At run-time an exception can be <EM>raised</EM>, which calls attention
  27. to the fact that an exceptional situation has occurred.
  28. <P>
  29. The default action when an exception is raised is to halt the program.
  30. Usually the program will print out the name of the exception and where
  31. the problem took place, though this depends on your compiler.
  32. The next few sections will show how to override this default.
  33. <P>
  34. If you don't want to halt the program, you'll need to tell Ada what to do
  35. instead by defining an <EM>exception handler</EM>.
  36. An exception handler states what exceptions to handle and what
  37. to do when a given exception is raised.
  38. <P>
  39. Exceptions generally represent something unusual and not normally expected -
  40. reserve their use for things like serious error conditions.
  41. They shouldn't be used for ``expected'' situations, because
  42. they can be slower and if incorrectly handled can stop a program.
  43. The place where an exception is raised may be far away from where it is
  44. handled, and that makes programs with a voluminous number of different
  45. exceptions harder to understand.
  46. Instead, exceptions should be used when a subprogram cannot perform its job
  47. for some significant reason.
  48. <P>
  49. Ada has a number of <EM>predefined</EM> exceptions that are raised when
  50. certain language-defined checks fail.
  51. The predefined check you're most likely to see is
  52. <EM>Constraint_Error</EM>;
  53. this exception is raised when a value goes out-of-bounds for its type.
  54. Examples of this include trying to store a value
  55. that's too large or too small into that type,
  56. dividing by zero, or using an invalid array index.
  57. <P>
  58. Naturally, there is some run-time overhead in performing all these checks,
  59. though less than you might think.
  60. It is possible to suppress these language-defined checks;
  61. this should only be done after the program is thoroughly debugged, and
  62. many people think that it shouldn't be done even then.
  63. <P>
  64. Some packages define their own exceptions, for example, Text_IO
  65. defines the exception <EM>End_Error</EM> that is raised when you attempt to
  66. ``Get'' something after you've reached the end of the file, and
  67. <EM>Name_Error</EM> is raised if try to open a file that doesn't exist.
  68. <P>
  69. In the next few sections we'll learn how to define exceptions,
  70. how to raise exceptions, and how to handle exceptions.
  71.  
  72. <QUESTION Type=Multiple-Choice>
  73. If you're defining a package that displays the view from an airplane
  74. cockpit, should you
  75. raise an exception whenever the view changes from daytime to nighttime?
  76. <CHOICES>
  77. <CHOICE ANS=1>You should probably raise an exception.
  78. <CHOICE ANS=2>You should probably <EM>not</EM> raise an exception.
  79. </CHOICES>
  80. <ANSWER ANS=2>
  81. <RESPONSES>
  82. <WHEN ANS=1>
  83. Sorry, that's probably not a good approach.
  84. You should only use exceptions when serious problems arise, not
  85. just when some interesting state changes.
  86. It <EM>might</EM> be a good approach, but it probably isn't.
  87. </RESPONSES>
  88. <SECTION NAME="Declaring Exceptions">
  89. Before you can raise or handle an exception, it must be declared.
  90. Declaring exceptions is just like declaring a variable of
  91. type <EM>exception</EM>; here's an example:
  92. <P>
  93. <PRE>
  94.   Singularity : exception;
  95. </PRE>
  96. <P>
  97. To be complete, here's the syntax
  98. for defining an exception, describing using
  99. <A HREF="bnf.htm">BNF</A>:
  100. <P>
  101. <PRE>
  102.   exception_declaration ::= defining_identifier_list ": exception;"
  103.   defining_identifier_list ::= identifier { "," identifier }
  104. </PRE>
  105. <P>
  106. Exception declarations are generally placed in a package declaration.
  107. <P>
  108. Raising an exception is easy, too - just use the <EM>raise</EM>
  109. statement.
  110. A raise statement is simply the keyword "raise" followed by the name
  111. of the exception; the syntax in <A HREF="bnf.htm">BNF</A> is:
  112. <P>
  113. <PRE>
  114.   raise_statement ::= "raise" [ exception_name ] ";"
  115. </PRE>
  116. <P>
  117. You'll notice that the exception_name is optional; we'll discuss
  118. what that means in the next section.
  119.  
  120. <SECTION NAME="Handling Exceptions">
  121. As we've noted many times, if an exception is raised and isn't handled
  122. the program stops.
  123. To handle exceptions you must define, reasonably enough, an
  124. <EM>exception handler</EM>.
  125. <P>
  126. When an exception is raised (by the raise statement) Ada will abandon
  127. what it was doing and look for a matching exception handler
  128. in the sequence of statements where the exception was raised.
  129. A sequence of statements is the set of statements between the
  130. keywords "begin" and "end".
  131. If Ada doesn't find a match, it returns from the current subprogram
  132. (cleaning up along the way) and tries to find a matching exception handler
  133. in the caller (from where it was called).
  134. If it doesn't find one there, it exits that subprogram
  135. (cleaning up along the way) and tries yet again.
  136. Ada keeps repeating this process until it finds a matching exception handler
  137. or until it exits the program.
  138. <P>
  139. An exception handler is defined just before the "end" statement
  140. that matches a "begin" statement.
  141. <P>
  142. For example, here's a procedure that "Open"s a file
  143. if it can, and if the file doesn't exist it "Create"s it.
  144. <P>
  145. <PRE>
  146.   procedure Open_Or_Create(File : in out File_Type;
  147.                            Mode : File_Mode; Name : String) is
  148.   begin
  149.     -- Try to open the file. This will raise Name_Error if
  150.     -- it doesn't exist.
  151.     Open(File, Mode, Name);
  152.   exception
  153.     when Name_Error =>
  154.       Create(File, Mode, Name);
  155.   end Open_Or_Create;
  156. </PRE>
  157. <P>
  158. Here's a simplified <A HREF="bnf.htm">BNF</A> of an exception handler:
  159. <P>
  160. <PRE>
  161.   exception_handler ::= exception_choice { "|"  "," exception_choice } "=>"
  162.                         sequence_of_statements
  163.   exception_choice  ::= exception_name | "others"
  164. </PRE>
  165. <P>
  166. The keyword "others" means all exceptions not explicitly listed
  167. in this exception handler; thus, you can handle all exceptions if you want to.
  168. <P>
  169. Inside an exception handler you can do any kind of processing you wish.
  170. If, after processing, you find you need to raise the same exception to
  171. a higher level, you can use the "raise" statement without the
  172. name of an exception.
  173. A raise statement without a named exception <EM>re-raises</EM> the
  174. exception being handled.
  175. Raise statements can only re-raise exceptions inside an exception handler.
  176. <P>
  177. You can pass information along with an exception,
  178. and there is a predefined package of exception-related operations.
  179. We won't go into that now, but if you're 
  180. interested, you can examine
  181. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-toc-11.html">section 11
  182. of the Ada LRM, which discusses exceptions</A>.
  183.