home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / lrm / chap11.doc < prev    next >
Encoding:
Text File  |  1988-05-03  |  37.5 KB  |  1,586 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. The following document is a draft  of  the  corresponding  chapter  of  the
  7. version  of  the  Ada  Reference  Manual  produced  in response to the Ansi
  8. Canvass.  It is given a limited circulation  to  Ada  implementers  and  to
  9. other groups contributing comments (according to the conventions defined in
  10. RRM.comments).  This draft should not be referred to in any publication.
  11.  
  12.  
  13.  
  14.                       ANSI-RM-11-v23 - Draft Chapter
  15.  
  16.                               11  Exceptions
  17.                                 version 23
  18.  
  19.                                  83-02-11
  20.  
  21.         This version has addressed all comments up to number #5795
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.                               11. Exceptions
  78.  
  79.  
  80.  
  81. This  chapter  defines  the  facilities  for  dealing  with errors or other
  82. exceptional  situations  that  arise  during  program  execution.   Such  a
  83. situation  is  called  an  exception.   To raise an exception is to abandon
  84. normal program execution so as to draw  attention  to  the  fact  that  the
  85. corresponding situation has arisen.  Executing some actions, in response to
  86. the arising of an exception, is called handling the exception.
  87.  
  88. An  exception  declaration  declares a name for an exception.  An exception
  89. can be raised by a  raise  statement,  or  it  can  be  raised  by  another
  90. statement  or  operation  that propagates the exception.  When an exception
  91. arises, control can be transferred to a user-provided exception handler  at
  92. the  end  of  a  block statement or at the end of the body of a subprogram,
  93. package, or task unit.
  94.  
  95. References:  block statement 5.6, error situation  1.6,  exception  handler
  96. 11.2,  name  4.1,  package  body  7.1,  propagation  of an exception 11.4.1
  97. 11.4.2, raise statement 11.3, subprogram body 6.3, task body 9.1
  98.  
  99.  
  100.  
  101.  
  102. 11.1  Exception Declarations
  103.  
  104.  
  105. An exception declaration declares a name for an exception.  The name of  an
  106. exception  can  only  be  used in raise statements, exception handlers, and
  107. renaming declarations.
  108.  
  109.     exception_declaration ::= identifier_list : exception;
  110.  
  111. An exception declaration  with  several  identifiers  is  equivalent  to  a
  112. sequence  of  single  exception  declarations, as explained in section 3.2.
  113. Each  single  exception  declaration  declares  a  name  for  a   different
  114. exception.   In  particular,  if  a  generic  unit  includes  an  exception
  115. declaration, the exception declarations implicitly generated  by  different
  116. instantiations  of  the  generic unit refer to distinct exceptions (but all
  117. have  the  same  identifier).   The  particular  exception  denoted  by  an
  118. exception name is determined at compilation time and is the same regardless
  119. of  how  many  times the exception declaration is elaborated.  Hence, if an
  120. exception declaration occurs in a recursive subprogram, the exception  name
  121. denotes the same exception for all invocations of the recursive subprogram.
  122.  
  123. The  following  exceptions are predefined in the language;  they are raised
  124. when the situations described are detected.
  125.  
  126.  
  127.  
  128.  
  129.                                   11 - 1
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. CONSTRAINT_ERROR  This  exception  is  raised  in  any  of  the   following
  139.                   situations:    upon   an   attempt  to  violate  a  range
  140.                   constraint,  an  index  constraint,  or  a   discriminant
  141.                   constraint;   upon  an  attempt to use a record component
  142.                   that does not exist for the current discriminant  values;
  143.                   and  upon  an  attempt  to  use  a selected component, an
  144.                   indexed component, a slice, or an attribute, of an object
  145.                   designated by an access value, if  the  object  does  not
  146.                   exist because the access value is null.
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                   11 - 2
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.    NUMERIC_ERROR  This exception is raised by the execution of a predefined
  205.                   numeric  operation  that  cannot deliver a correct result
  206.                   (within the declared  accuracy  for  real  types);   this
  207.                   includes   the   case  where  an  implementation  uses  a
  208.                   predefined   numeric   operation   for   the   execution,
  209.                   evaluation,  or elaboration of some construct.  The rules
  210.                   given in section 4.5.7  define  the  cases  in  which  an
  211.                   implementation  is  not  required to raise this exception
  212.                   when such an error situation arises;   see  also  section
  213.                   11.6.
  214.  
  215.    PROGRAM_ERROR  This exception is  raised  upon  an  attempt  to  call  a
  216.                   subprogram, to activate a task, or to elaborate a generic
  217.                   instantiation,  if the body of the corresponding unit has
  218.                   not yet been elaborated.  This exception is  also  raised
  219.                   if the end of a function is reached (see 6.5);  or during
  220.                   the  execution of a selective wait that has no else part,
  221.                   if this execution determines that  all  alternatives  are
  222.                   closed   (see   9.7.1).    Finally,   depending   on  the
  223.                   implementation, this exception  may  be  raised  upon  an
  224.                   attempt  to  execute an action that is erroneous, and for
  225.                   incorrect order dependences (see 1.6).
  226.  
  227.    STORAGE_ERROR  This  exception  is  raised  in  any  of  the   following
  228.                   situations:  when the dynamic storage allocated to a task
  229.                   is  exceeded;   during the evaluation of an allocator, if
  230.                   the space  available  for  the  collection  of  allocated
  231.                   objects  is  exhausted;   or  during the elaboration of a
  232.                   declarative item, or during the execution of a subprogram
  233.                   call, if storage is not sufficient.
  234.  
  235.    TASKING_ERROR  This exception is raised  when  exceptions  arise  during
  236.                   intertask communication (see 9 and 11.5).
  237.  
  238. Note:
  239.  
  240. The  situations described above can arise without raising the corresponding
  241. exceptions, if the pragma SUPPRESS has been used to give permission to omit
  242. the corresponding checks (see 11.7).
  243.  
  244. Examples of user-defined exception declarations:
  245.  
  246.     SINGULAR : exception;
  247.     ERROR    : exception;
  248.     OVERFLOW, UNDERFLOW : exception;
  249.  
  250. References:  access value 3.8, collection 3.8, declaration  3.1,  exception
  251. 11,  exception handler 11.2, generic body 12.2, generic instantiation 12.3,
  252. generic unit 12, identifier 2.3, implicit declaration  12.3,  instantiation
  253. 12.3,  name  4.1, object 3.2, raise statement 11.3, real type 3.5.6, record
  254. component 3.7, return statement 5.8, subprogram  6,  subprogram  body  6.3,
  255. task 9, task body 9.1
  256.  
  257. Constraint_error exception contexts:  aggregate 4.3.1 4.3.2, allocator 4.8,
  258. assignment  statement  5.2 5.2.1, constraint 3.3.2, discrete type attribute
  259.  
  260.  
  261.                                   11 - 3
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. 3.5.5, discriminant constraint  3.7.2,  elaboration  of  a  generic  formal
  271. parameter  12.3.1  12.3.2  12.3.4  12.3.5,  entry index 9.5, exponentiating
  272. operator 4.5.6, index constraint 3.6.1, indexed  component  4.1.1,  logical
  273. operator  4.5.1, null access value 3.8, object declaration 3.2.1, parameter
  274. association 6.4.1, qualified expression 4.7, range constraint 3.5, selected
  275. component 4.1.3, slice 4.1.2, subtype indication 3.3.2, type conversion 4.6
  276.  
  277. Numeric_error exception contexts:  discrete type attribute 3.5.5,  implicit
  278. conversion  3.5.4 3.5.6 4.6, numeric operation 3.5.5 3.5.8 3.5.10, operator
  279. of a numeric type 4.5 4.5.7
  280.  
  281. Program_error  exception  contexts:   collection  3.8,   elaboration   3.9,
  282. elaboration  check  3.9  7.3  9.3  12.2,  erroneous  1.6,  incorrect  order
  283. dependence 1.6, leaving a function 6.5, selective wait 9.7.1
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                   11 - 4
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. Storage_error exception contexts:  allocator 4.8
  337.  
  338. Tasking error exception contexts:  abort statement  9.10,  entry  call  9.5
  339. 9.7.2 9.7.3, exceptions during task communication 11.5, task activation 9.3
  340.  
  341.  
  342.  
  343.  
  344. 11.2  Exception Handlers
  345.  
  346.  
  347. The  response  to  one  or  more  exceptions  is  specified by an exception
  348. handler.
  349.  
  350.     exception_handler ::=
  351.        when exception_choice {| exception_choice} =>
  352.           sequence_of_statements
  353.  
  354.     exception_choice ::= exception_name | others
  355.  
  356. An exception handler occurs in a construct that is either a block statement
  357. or the body of a subprogram, package, task unit, or generic unit.   Such  a
  358. construct  will be called a frame in this chapter.  In each case the syntax
  359. of a frame that has exception handlers includes the following part:
  360.  
  361.     begin
  362.         sequence_of_statements
  363.     exception
  364.         exception_handler
  365.        {exception_handler}
  366.     end
  367.  
  368. The exceptions denoted by the exception names given as exception choices of
  369. a frame must all be distinct.  The exception choice others is only  allowed
  370. for the last exception handler of a frame and as its only exception choice;
  371. it  stands for all exceptions not listed in previous handlers of the frame,
  372. including exceptions whose names are  not  visible  at  the  place  of  the
  373. exception handler.
  374.  
  375. The  exception handlers of a frame handle exceptions that are raised by the
  376. execution of the sequence of  statements  of  the  frame.   The  exceptions
  377. handled  by  a given exception handler are those named by the corresponding
  378. exception choices.
  379.  
  380. Example:
  381.  
  382.     begin
  383.        --  sequence of statements
  384.     exception
  385.        when SINGULAR | NUMERIC_ERROR =>
  386.           PUT(" MATRIX IS SINGULAR ");
  387.        when others =>
  388.           PUT(" FATAL ERROR ");
  389.           raise ERROR;
  390.     end;
  391.  
  392.  
  393.                                   11 - 5
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402. Note:
  403.  
  404. The same kinds of statement are allowed in the sequence  of  statements  of
  405. each  exception handler as are allowed in the sequence of statements of the
  406. frame.   For example, a return statement is allowed in a handler  within  a
  407. function body.
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                   11 - 6
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. References:   block  statement  5.6,  declarative  part  3.9, exception 11,
  470. exception handling 11.4, function body 6.3, generic body 12.2, generic unit
  471. 12.1, name 4.1, package body 7.1, raise statement  11.3,  return  statement
  472. 5.8,  sequence  of  statements  5.1, statement 5, subprogram body 6.3, task
  473. body 9.1, task unit 9 9.1, visibility 8.3
  474.  
  475.  
  476.  
  477.  
  478. 11.3  Raise Statements
  479.  
  480.  
  481. A raise statement raises an exception.
  482.  
  483.     raise_statement ::= raise [exception_name];
  484.  
  485. For the execution of a raise statement with an exception  name,  the  named
  486. exception  is  raised.  A raise statement without an exception name is only
  487. allowed within an  exception  handler  (but  not  within  the  sequence  of
  488. statements  of  a subprogram, package, task unit, or generic unit, enclosed
  489. by the handler);  it raises again the exception that caused transfer to the
  490. innermost enclosing handler.
  491.  
  492. Examples:
  493.  
  494.     raise SINGULAR;
  495.     raise NUMERIC_ERROR;  --  explicitly raising a predefined exception
  496.  
  497.     raise;                --  only within an exception handler
  498.  
  499. References:  exception 11, generic unit 12, name 4.1, package  7,  sequence
  500. of statements 5.1, subprogram 6, task unit 9
  501.  
  502.  
  503.  
  504.  
  505. 11.4  Exception Handling
  506.  
  507.  
  508. When  an  exception  is  raised,  normal program execution is abandoned and
  509. control is transferred to an exception  handler.   The  selection  of  this
  510. handler  depends on whether the exception is raised during the execution of
  511. statements or during the elaboration of declarations.
  512.  
  513. References:  declaration 3.1, elaboration 3.1 3.9, exception 11,  exception
  514. handler 11.2, raising of exceptions 11.3, statement 5
  515.  
  516.  
  517.  
  518.  
  519. 11.4.1  Exceptions Raised During the Execution of Statements
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                   11 - 7
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534. The  handling  of  an  exception  raised  by the execution of a sequence of
  535. statements depends on whether the innermost frame or accept statement  that
  536. encloses the sequence of statements is a frame or an accept statement.  The
  537. case  where  an accept statement is innermost is described in section 11.5.
  538. The case where a frame is innermost is presented here.
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                   11 - 8
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600. Different actions take place, depending on whether or not this frame has  a
  601. handler  for  the  exception, and on whether the exception is raised in the
  602. sequence of statements of the frame or in that of an exception handler.
  603.  
  604. If an exception is raised in the sequence of statements of a frame that has
  605. a handler for the exception, execution of the sequence of statements of the
  606. frame is abandoned and control is transferred  to  the  exception  handler.
  607. The  execution  of  the sequence of statements of the handler completes the
  608. execution of the frame (or its elaboration if the frame is a package body).
  609.  
  610. If an exception is raised in the sequence of statements  of  a  frame  that
  611. does  not  have  a handler for the exception, execution of this sequence of
  612. statements is abandoned.  The next action depends  on  the  nature  of  the
  613. frame:
  614.  
  615. (a)  For a subprogram body, the same exception is raised again at the point
  616.      of  call  of the subprogram, unless the subprogram is the main program
  617.      itself, in which case execution of the main program is abandoned.
  618.  
  619. (b)  For a block statement, the same exception is raised again  immediately
  620.      after  the  block  statement  (that is, within the innermost enclosing
  621.      frame or accept statement).
  622.  
  623. (c)  For a package body that is a declarative item, the same  exception  is
  624.      raised  again  immediately  after  this  declarative  item (within the
  625.      enclosing declarative part). If the package body is that of a subunit,
  626.      the exception is raised again at the place of the  corresponding  body
  627.      stub.  If the package is a library unit, execution of the main program
  628.      is abandoned.
  629.  
  630. (d)  For a task body, the task becomes completed.
  631.  
  632. An exception that is raised again (as in the above cases (a), (b), and (c))
  633. is  said  to  be propagated, either by the execution of the subprogram, the
  634. execution of the block statement, or the elaboration of the  package  body.
  635. No  propagation  takes place in the case of a task body.  If the frame is a
  636. subprogram or a  block  statement  and  if  it  has  dependent  tasks,  the
  637. propagation  of  an  exception  takes  place  only after termination of the
  638. dependent tasks.
  639.  
  640. Finally, if an exception is raised in the  sequence  of  statements  of  an
  641. exception  handler,  execution of this sequence of statements is abandoned.
  642. Subsequent actions (including propagation, if any) are as in the cases  (a)
  643. to (d) above, depending on the nature of the frame.
  644.  
  645. Example:
  646.  
  647.     function FACTORIAL (N : POSITIVE) return FLOAT is
  648.     begin
  649.        if N = 1 then
  650.           return 1.0;
  651.        else
  652.           return FLOAT(N) * FACTORIAL(N-1);
  653.        end if;
  654.     exception
  655.  
  656.  
  657.                                   11 - 9
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.        when NUMERIC_ERROR => return FLOAT'SAFE_LARGE;
  667.     end FACTORIAL;
  668.  
  669. If  the  multiplication  raises  NUMERIC_ERROR,  then  FLOAT'SAFE_LARGE  is
  670. returned by the handler.   This  value  will  cause  further  NUMERIC_ERROR
  671. exceptions  to be raised by the evaluation of the expression in each of the
  672. remaining invocations of the function, so that for large values  of  N  the
  673. function will ultimately return the value FLOAT'SAFE_LARGE.
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                   11 - 10
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733. Example:
  734.  
  735.     procedure P is
  736.        ERROR : exception;
  737.        procedure R;
  738.  
  739.        procedure Q is
  740.        begin
  741.           R;
  742.           ...            --  error situation (2)
  743.        exception
  744.           ...
  745.           when ERROR =>  --  handler E2
  746.           ...
  747.        end Q;
  748.  
  749.        procedure R is
  750.        begin
  751.           ...            --  error situation (3)
  752.        end R;
  753.  
  754.     begin
  755.        ...               --  error situation (1)
  756.        Q;
  757.        ...
  758.     exception
  759.        ...
  760.        when ERROR =>     --  handler E1
  761.        ...
  762.     end P;
  763.  
  764. The following situations can arise:
  765.  
  766. (1)  If the exception ERROR is raised in the sequence of statements of  the
  767.      outer  procedure  P,  the  handler  E1  provided  within  P is used to
  768.      complete the execution of P.
  769.  
  770. (2)  If the exception ERROR is raised in the sequence of statements  of  Q,
  771.      the  handler E2 provided within Q is used to complete the execution of
  772.      Q.  Control will be returned to the point of call of Q upon completion
  773.      of the handler.
  774.  
  775. (3)  If the exception ERROR is raised in the body of R, called  by  Q,  the
  776.      execution  of  R  is abandoned and the same exception is raised in the
  777.      body of Q.  The handler E2 is then used to complete the  execution  of
  778.      Q, as in situation (2).
  779.  
  780. Note  that  in  the  third  situation, the exception raised in R results in
  781. (indirectly) transferring control to a handler that is part of Q and  hence
  782. not  enclosed by R.  Note also that if a handler were provided within R for
  783. the exception choice others, situation (3) would cause  execution  of  this
  784. handler, rather than direct termination of R.
  785.  
  786.  
  787.  
  788.  
  789.                                   11 - 11
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798. Lastly,  if ERROR had been declared in R, rather than in P, the handlers E1
  799. and E2  could  not  provide  an  explicit  handler  for  ERROR  since  this
  800. identifier would not be visible within the bodies of P and Q.  In situation
  801. (3), the exception could however be handled in Q by providing a handler for
  802. the exception choice others.
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.                                   11 - 12
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864. Notes:
  865.  
  866. The  language  does  not define what happens when the execution of the main
  867. program is abandoned after an unhandled exception.
  868.  
  869. The predefined exceptions are those that can be  propagated  by  the  basic
  870. operations and the predefined operators.
  871.  
  872. The  case of a frame that is a generic unit is already covered by the rules
  873. for subprogram and package bodies, since the sequence of statements of such
  874. a frame is not executed but is the template for the corresponding sequences
  875. of  statements  of  the  subprograms  or  packages  obtained   by   generic
  876. instantiation.
  877.  
  878. References:   accept  statement 9.5, basic operation 3.3.3, block statement
  879. 5.6, body stub 10.2, completion 9.4, declarative item 3.9, declarative part
  880. 3.9, dependent task 9.4,  elaboration  3.1  3.9,  exception  11,  exception
  881. handler  11.2,  frame  11.2,  generic  instantiation 12.3, generic unit 12,
  882. library unit 10.1, main program 10.1, numeric_error exception 11.1, package
  883. 7, package body 7.1, predefined operator 4.5, procedure  6.1,  sequence  of
  884. statements  5.1, statement 5, subprogram 6, subprogram body 6.3, subprogram
  885. call 6.4, subunit 10.2, task 9, task body 9.1
  886.  
  887.  
  888.  
  889.  
  890. 11.4.2  Exceptions Raised During the Elaboration of Declarations
  891.  
  892.  
  893. If an exception is raised during the elaboration of the declarative part of
  894. a given frame, this elaboration is abandoned.  The next action  depends  on
  895. the nature of the frame:
  896.  
  897. (a)  For a subprogram body, the same exception is raised again at the point
  898.      of  call  of the subprogram, unless the subprogram is the main program
  899.      itself, in which case execution of the main program is abandoned.
  900.  
  901. (b)  For a block statement, the same exception is raised again  immediately
  902.      after the block statement.
  903.  
  904. (c)  For a package body that is a declarative item, the same  exception  is
  905.      raised again immediately after this declarative item, in the enclosing
  906.      declarative  part.   If  the  package  body  is that of a subunit, the
  907.      exception is raised again at the place of the corresponding body stub.
  908.      If the package is a library unit, execution of  the  main  program  is
  909.      abandoned.
  910.  
  911. (d)  For a task  body,  the  task  becomes  completed,  and  the  exception
  912.      TASKING_ERROR  is  raised  at  the point of activation of the task, as
  913.      explained in section 9.3.
  914.  
  915. Similarly, if an exception is raised during the  elaboration  of  either  a
  916. package  declaration  or a task declaration, this elaboration is abandoned;
  917. the next action depends on the nature of the declaration.
  918.  
  919.  
  920.  
  921.                                   11 - 13
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930. (e)  For a package declaration or a task declaration, that is a declarative
  931.      item, the exception is raised again immediately after the  declarative
  932.      item  in the enclosing declarative part or package specification.  For
  933.      the declaration of a  library  package,  the  execution  of  the  main
  934.      program is abandoned.
  935.  
  936. An  exception that is raised again (as in the above cases (a), (b), (c) and
  937. (e)) is said to be propagated, either by the execution of the subprogram or
  938. block statement, or by the elaboration of  the  package  declaration,  task
  939. declaration or package body.
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                   11 - 14
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997. Example  of an exception in the declarative part of a block statement (case
  998. (b)):
  999.  
  1000.     procedure P is
  1001.        ...
  1002.     begin
  1003.        declare
  1004.           N : INTEGER := F;  --  the function F may raise ERROR
  1005.        begin
  1006.           ...
  1007.        exception
  1008.           when ERROR =>      --  handler E1
  1009.        end;
  1010.        ...
  1011.     exception
  1012.        when ERROR =>         --  handler E2
  1013.     end P;
  1014.  
  1015.     --  if the exception ERROR is raised in the declaration of N, it is handled by E2
  1016.  
  1017. References:  activation 9.3, block statement 5.6, body stub 10.2, completed
  1018. task 9.4, declarative item 3.9, declarative part 3.9, elaboration 3.1  3.9,
  1019. exception  11,  frame  11.2,  library unit 10.1, main program 10.1, package
  1020. body 7.1, package declaration 7.1, package specification 7.1, subprogram 6,
  1021. subprogram body 6.3, subprogram call 6.4, subunit 10.2, task 9,  task  body
  1022. 9.1, task declaration 9.1, tasking_error exception 11.1
  1023.  
  1024.  
  1025.  
  1026.  
  1027. 11.5  Exceptions Raised During Task Communication
  1028.  
  1029.  
  1030. An  exception  can  be propagated to a task communicating, or attempting to
  1031. communicate, with another task.  An exception can also be propagated  to  a
  1032. calling task if the exception is raised during a rendezvous.
  1033.  
  1034. When  a task calls an entry of another task, the exception TASKING_ERROR is
  1035. raised in the calling task, at the place of the call, if the called task is
  1036. completed before accepting the entry call or is already  completed  at  the
  1037. time of the call.
  1038.  
  1039. A rendezvous can be completed abnormally in two cases:
  1040.  
  1041. (a)  When an exception is  raised  within  an  accept  statement,  but  not
  1042.      handled  within  an  inner  frame.  In this case, the execution of the
  1043.      accept statement is abandoned and the same exception is  raised  again
  1044.      immediately  after  the  accept  statement within the called task; the
  1045.      exception is also propagated to the calling task at the point  of  the
  1046.      entry call.
  1047.  
  1048. (b)  When the task containing the accept statement is completed  abnormally
  1049.      as  the  result  of  an  abort statement.  In this case, the exception
  1050.      TASKING_ERROR is raised in the calling task at the point of the  entry
  1051.  
  1052.  
  1053.                                   11 - 15
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.      call.
  1063.  
  1064. On the other hand, if a task issuing an entry call becomes abnormal (as the
  1065. result  of  an  abort statement) no exception is raised in the called task.
  1066. If the rendezvous has not yet started, the entry call is cancelled.  If the
  1067. rendezvous is in progress, it completes normally, and the  called  task  is
  1068. unaffected.
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.                                   11 - 16
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128. References:   abnormal  task  9.10,  abort statement 9.10, accept statement
  1129. 9.5, completed  task  9.4,  entry  call  9.5,  exception  11,  frame  11.2,
  1130. rendezvous  9.5, task 9, task termination 9.4, tasking_error exception 11.1
  1131.  
  1132.  
  1133.  
  1134.  
  1135. 11.6  Exceptions and Optimization
  1136.  
  1137.  
  1138. The purpose of this section is to specify the  conditions  under  which  an
  1139. implementation  is  allowed  to  perform  certain actions either earlier or
  1140. later than specified by other rules of the language.
  1141.  
  1142. In general, when the language rules specify an order  for  certain  actions
  1143. (the  canonical order), an implementation may only use an alternative order
  1144. if it can guarantee that the effect of the program is not  changed  by  the
  1145. reordering.   In particular, no exception should arise for the execution of
  1146. the reordered program if none arises for the execution of  the  program  in
  1147. the canonical order.  When, on the other hand, the order of certain actions
  1148. is   not   defined   by  the  language,  any  order  can  be  used  by  the
  1149. implementation.  (For example, the arguments of a predefined  operator  can
  1150. be  evaluated  in  any   order  since the rules given in section 4.5 do not
  1151. require a specific order of evaluation.)
  1152.  
  1153. Additional freedom is left to  an  implementation  for  reordering  actions
  1154. involving  predefined  operations  that  are either predefined operators or
  1155. basic operations other than assignments.  This freedom is left, as  defined
  1156. below,  even in the case where the execution of these predefined operations
  1157. may propagate a (predefined) exception:
  1158.  
  1159. (a)  For the purpose of establishing whether the same effect is obtained by
  1160.      the  execution  of  certain  actions  in  the  canonical  and  in   an
  1161.      alternative  order,  it  can  be  assumed  that none of the predefined
  1162.      operations  invoked  by  these  actions  propagates   a   (predefined)
  1163.      exception, provided that the two following requirements are met by the
  1164.      alternative  order:   first,  an  operation must not be invoked in the
  1165.      alternative order if  it  is  not  invoked  in  the  canonical  order;
  1166.      second,  for  each  operation, the innermost enclosing frame or accept
  1167.      statement must be  the  same  in  the  alternative  order  as  in  the
  1168.      canonical order, and the same exception handlers must apply.
  1169.  
  1170. (b)  Within an expression, the association of operators  with  operands  is
  1171.      specified  by  the  syntax.   However,  for  a  sequence of predefined
  1172.      operators of  the  same  precedence  level  (and  in  the  absence  of
  1173.      parentheses  imposing  a  specific  association),  any  association of
  1174.      operators with operands is  allowed  if  it  satisfies  the  following
  1175.      requirement:   an  integer  result  must be equal to that given by the
  1176.      canonical left-to-right order;  a  real  result  must  belong  to  the
  1177.      result  model  interval  defined for the canonical left-to-right order
  1178.      (see 4.5.7).  Such a reordering is allowed even if it  may  remove  an
  1179.      exception, or introduce a further predefined exception.
  1180.  
  1181. Similarly,  additional  freedom  is  left  to  an  implementation  for  the
  1182. evaluation  of  numeric  simple  expressions.   For  the  evaluation  of  a
  1183.  
  1184.  
  1185.                                   11 - 17
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194. predefined  operation, an implementation is allowed to use the operation of
  1195. a type that has a range wider than that of the base type of  the  operands,
  1196. provided  that  this  delivers  the  exact  result  (or a result within the
  1197. declared accuracy, in the case of a real type), even if  some  intermediate
  1198. results   lie   outside   the  range  of  the  base  type.   The  exception
  1199. NUMERIC_ERROR need not be raised in such a case.   In  particular,  if  the
  1200. numeric  expression  is an operand of a predefined relational operator, the
  1201. exception NUMERIC_ERROR need  not  be  raised  by  the  evaluation  of  the
  1202. relation, provided that the correct BOOLEAN result is obtained.
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.                                   11 - 18
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260. A  predefined  operation  need  not be invoked at all, if its only possible
  1261. effect is to propagate a predefined  exception.   Similarly,  a  predefined
  1262. operation  need  not  be invoked if the removal of subsequent operations by
  1263. the above rule renders this invocation ineffective.
  1264.  
  1265. Notes:
  1266.  
  1267. Rule (b) applies to predefined  operators  but  not  to  the  short-circuit
  1268. control forms.
  1269.  
  1270. The  expression  SPEED  <  300_000.0  can  be replaced by TRUE if the value
  1271. 300_000.0 lies outside the base type of SPEED,  even  though  the  implicit
  1272. conversion  of the numeric literal would raise the exception NUMERIC_ERROR.
  1273.  
  1274. Example:
  1275.  
  1276.     declare
  1277.        N : INTEGER;
  1278.     begin
  1279.        N := 0;               --  (1)
  1280.        for J in 1 .. 10 loop
  1281.           N := N + J**A(K);  --  A and K are global variables
  1282.        end loop;
  1283.        PUT(N);
  1284.     exception
  1285.        when others => PUT("Some error arose"); PUT(N);
  1286.     end;
  1287.  
  1288. The evaluation of A(K) may be  performed  before  the  loop,  and  possibly
  1289. immediately before the assignment statement (1) even if this evaluation can
  1290. raise  an exception.  Consequently, within the exception handler, the value
  1291. of N is either the undefined initial value or a value later  assigned.   On
  1292. the  other  hand, the evaluation of A(K) cannot be moved before begin since
  1293. an exception would then be  handled  by  a  different  handler.   For  this
  1294. reason, the initialization of N in the declaration itself would exclude the
  1295. possibility of having an undefined initial value of N in the handler.
  1296.  
  1297. References:   accept  statement  9.5,  accuracy  of  real operations 4.5.7,
  1298. assignment 5.2, base type 3.3, basic operation 3.3.3, conversion 4.6, error
  1299. situation  11,  exception  11,  exception   handler   11.2,   frame   11.2,
  1300. numeric_error   exception   11.1,   predefined   operator  4.5,  predefined
  1301. subprogram  8.6,  propagation  of  an  exception  11.4,  real  type  3.5.6,
  1302. undefined value 3.2.1
  1303.  
  1304.  
  1305.  
  1306.  
  1307. 11.7  Suppressing Checks
  1308.  
  1309.  
  1310. The  presence of a SUPPRESS pragma gives permission to an implementation to
  1311. omit certain run-time checks.  The form of this pragma is as follows:
  1312.  
  1313.     pragma SUPPRESS(identifier [, [ON =>] name]);
  1314.  
  1315.  
  1316.  
  1317.                                   11 - 19
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326. The identifier is that of the check that can  be  omitted.   The  name  (if
  1327. present)  must  be  either  a  simple  name or an expanded name and it must
  1328. denote either an object, a type or subtype, a task unit, or a generic unit;
  1329. alternatively the name can be a subprogram name, in which case it can stand
  1330. for several visible overloaded subprograms.
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.                                   11 - 20
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392. A pragma SUPPRESS is only allowed immediately within a declarative part  or
  1393. immediately  within  a package specification.  In the latter case, the only
  1394. allowed form is with a name that denotes an entity (or  several  overloaded
  1395. subprograms)  declared  immediately  within the package specification.  The
  1396. permission to omit the given check extends from the place of the pragma  to
  1397. the  end  of the declarative region associated with the innermost enclosing
  1398. block statement  or  program  unit.   For  a  pragma  given  in  a  package
  1399. specification,  the permission extends to the end of the scope of the named
  1400. entity.
  1401.  
  1402. If the pragma includes a name, the permission to omit the  given  check  is
  1403. further restricted:  it is given only for operations on the named object or
  1404. on all objects of the base type of a named type or subtype;  for calls of a
  1405. named subprogram;  for activations of tasks of the named task type;  or for
  1406. instantiations of the given generic unit.
  1407.  
  1408. The  following  checks  correspond  to  situations  in  which the exception
  1409. CONSTRAINT_ERROR may be raised; for these checks,  the  name  (if  present)
  1410. must denote either an object or a type.
  1411.  
  1412. ACCESS_CHECK          When  accessing  a  selected  component,  an  indexed
  1413.                       component,  a  slice,  or  an attribute, of an object
  1414.                       designated by an access value, check that the  access
  1415.                       value is not null.
  1416.  
  1417. DISCRIMINANT_CHECK    Check that a discriminant of a  composite  value  has
  1418.                       the  value  imposed  by  a  discriminant  constraint.
  1419.                       Also, when accessing a record component,  check  that
  1420.                       it exists for the current discriminant values.
  1421.  
  1422. INDEX_CHECK           Check that the bounds of an array value are equal  to
  1423.                       the  corresponding  bounds  of  an  index constraint.
  1424.                       Also, when accessing a component of an array  object,
  1425.                       check  for  each dimension that the given index value
  1426.                       belongs to the range defined by  the  bounds  of  the
  1427.                       array  object.   Also,  when  accessing a slice of an
  1428.                       array object, check that the given discrete range  is
  1429.                       compatible  with  the  range defined by the bounds of
  1430.                       the array object.
  1431.  
  1432. LENGTH_CHECK          Check that there is a  matching  component  for  each
  1433.                       component   of   an  array,  in  the  case  of  array
  1434.                       assignments, type conversions, and logical  operators
  1435.                       for arrays of boolean components.
  1436.  
  1437. RANGE_CHECK           Check that a  value  satisfies  a  range  constraint.
  1438.                       Also,  for  the  elaboration of a subtype indication,
  1439.                       check that the constraint (if present) is  compatible
  1440.                       with  the  type  mark.  Also, for an aggregate, check
  1441.                       that an index or discriminant value  belongs  to  the
  1442.                       corresponding   subtype.    Finally,  check  for  any
  1443.                       constraint   checks   performed    by    a    generic
  1444.                       instantiation.
  1445.  
  1446.  
  1447.  
  1448.  
  1449.                                   11 - 21
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. The  following  checks  correspond  to  situations  in  which the exception
  1459. NUMERIC_ERROR is raised.  The  only  allowed  names  in  the  corresponding
  1460. pragmas are names of numeric types.
  1461.  
  1462. DIVISION_CHECK        Check that the second operand is  not  zero  for  the
  1463.                       operations /, rem and mod.
  1464.  
  1465. OVERFLOW_CHECK        Check that the result of a numeric operation does not
  1466.                       overflow.
  1467.  
  1468. The  following  check  corresponds  to  situations  in  which the exception
  1469. PROGRAM_ERROR is raised.  The  only  allowed  names  in  the  corresponding
  1470. pragmas are names denoting task units, generic units, or subprograms.
  1471.  
  1472. ELABORATION_CHECK     When either a subprogram is called, a task activation
  1473.                       is   accomplished,  or  a  generic  instantiation  is
  1474.                       elaborated, check that the body of the  corresponding
  1475.                       unit has already been elaborated.
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.                                   11 - 22
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524. The  following  check  corresponds  to  situations  in  which the exception
  1525. STORAGE_ERROR is raised.  The  only  allowed  names  in  the  corresponding
  1526. pragmas are names denoting access types, task units, or subprograms.
  1527.  
  1528. STORAGE_CHECK         Check that execution of an allocator does not require
  1529.                       more space than is available for a collection.  Check
  1530.                       that the space available for a task or subprogram has
  1531.                       not been exceeded.
  1532.  
  1533. If  an  error situation arises in the absence of the corresponding run-time
  1534. checks, the execution of the program is  erroneous  (the  results  are  not
  1535. defined by the language).
  1536.  
  1537. Examples:
  1538.  
  1539.     pragma SUPPRESS(RANGE_CHECK);
  1540.     pragma SUPPRESS(INDEX_CHECK, ON => TABLE);
  1541.  
  1542. Notes:
  1543.  
  1544. For certain implementations, it may be impossible or too costly to suppress
  1545. certain  checks.  The corresponding SUPPRESS pragma can be ignored.  Hence,
  1546. the occurrence of such a pragma within a given unit does not guarantee that
  1547. the corresponding exception will not arise;  the  exceptions  may  also  be
  1548. propagated by called units.
  1549.  
  1550.  
  1551. References:
  1552.  
  1553. access type 3.8, access value 3.8, activation 9.3, aggregate 4.3, allocator
  1554. 4.8,  array  3.6,  attribute  4.1.4,  block  statement 5.6, collection 3.8,
  1555. compatible 3.3.2, component of an array 3.6, component  of  a  record  3.7,
  1556. composite  type  3.3,  constraint  3.3,  constraint_error  exception  11.1,
  1557. declarative part 3.9, designate 3.8, dimension  3.6,  discrete  range  3.6,
  1558. discriminant  3.7.1,  discriminant  constraint  3.7.2, elaboration 3.1 3.9,
  1559. erroneous 1.6, error situation 11, expanded name 4.1.3, generic body  11.1,
  1560. generic  instantiation  12.3,  generic  unit 12, identifier 2.3, index 3.6,
  1561. index constraint 3.6.1, indexed component 4.1.1,  null  access  value  3.8,
  1562. numeric  operation  3.5.5  3.5.8  3.5.10,  numeric  type 3.5, numeric_error
  1563. exception 11.1, object 3.2, operation  3.3.3,  package  body  7.1,  package
  1564. specification  7.1,  pragma 2.8, program_error exception 11.1, program unit
  1565. 6, propagation of an exception 11.4, range constraint 3.5, record type 3.7,
  1566. simple name 4.1, slice 4.1.2, subprogram 6, subprogram body 6.3, subprogram
  1567. call 6.4, subtype 3.3, subunit 10.2, task 9, task body 9.1, task type  9.1,
  1568. task unit 9, type 3.3, type mark 3.3.2
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.                                   11 - 23
  1582.  
  1583.  
  1584.  
  1585.  
  1586.