[next] [previous] [contents]

  16.2.7 Handling Errors in Multiple-Unit Programs
  You can use WHEN ERROR constructs anywhere in your
  main program or program modules. Procedure and function
  invocations, such as invocations of DEF and DEF
                                                                        *

                                                                            functions
  and SUB, FUNCTION, and PICTURE subroutines, as well
  as non-BASIC programs, are valid within protected regions.
  GOTO and GOSUB statements are valid within handlers
  provided that the target is within the handler, an outer
  handler, or an unprotected region. Note, however, that a de-
  tached handler cannot appear within DEF or DEF
                                                                          *

                                                                              functions
  without the associated protected region.

  When an error occurs within nested protected regions, BASIC
  maintains the same priorities for handler use; control always
  passes to the handler associated with the innermost protected
  region in which the error occurred. When an exception oc-
  curs, all handlers for any outer WHEN ERROR blocks are
  processed before the program reverts to default error han-
  dling. Outer handlers are invoked when an inner handler
  executes an EXIT HANDLER statement. When there are no
  more outer handlers, and the outermost handler executes an
  EXIT HANDLER statement, program control reverts to the
  handler associated with the calling routine. For example:
  
EXAMPLE: Click to display example.

  You can call a subprogram while an error is pending; how-
  ever, if you do, the subprogram cannot resignal an error back
  to the calling program. If the subprogram tries to resig-
  nal an error, BASIC signals ``Improper error handling'' and
  program execution terminates.

  The following rules apply to error handling in function
  definitions:

  .
        DEF and DEF
                            *

                              function definitions cannot appear within
        a protected region. However, protected regions can be
        contained within the function definitions.
  .
        To trap errors while a DEF function is active, include
        protected regions inside the DEF function. If you do
        this, the associated handler remains in effect until your
        program leaves the protected region, or the DEF function.

  For example:
  
EXAMPLE: Click to display example.


                                            Note



        If you invoke a GOSUB statement or a DEF
                                                                      *

                                                                          function
        from within a protected region and the invoked pro-
        cedure is outside of any protected region, all pending
        errors are handled by the WHEN ERROR handler
        unless a previously executed ON ERROR statement
        specifies otherwise.