Microsoft KB Archive/41390

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


Handling Events and ERRORs When Basic Calls Non-Basic Routines

Article ID: 41390

Article Last Modified on 8/16/2005



APPLIES TO

  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft BASIC Professional Development System 7.0



This article was previously published under Q41390

SUMMARY

This information applies to QuickBasic Versions 4.00, 4.00b, and 4.50, Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS and OS/2, and Microsoft Basic PDS Version 7.00 for MS-DOS and MS OS/2.

The following corrections for documentation errors apply to Page 296 of the learning and using Microsoft QuickBasic manual for QuickBasic 4.00 and 4.00b and Basic compiler 6.00 and 6.00b, and to Page 442-443 of the "Microsoft Basic Version 7.0: Programmer's Guide" for Basic PDS 7.00:

  1. The section "Events and Errors" incorrectly implies that Basic can call a subroutine written in a different language, have that language sense an EVENT or ERROR, and then jump to the error handling routine in Basic. Basic is not designed to do this. What happens is that the first event (but not subsequent events) that occurs during the other language procedure is stored on the stack until control returns to Basic. If the other language procedure calls a Basic procedure in which the ERROR statement forces an error (or a natural error occurs), then the error handler (if any) in the Basic program handles the error and RESUMEs as described below.
  2. The last sentence is incorrect ("The Basic statement containing... is the statement that RESUME would reexecute"). This sentence should be changed to say the following:


"The Basic statement containing the error (ERROR x%) is the statement that RESUME would reexecute. RESUME NEXT would reexecute at the following statement."


MORE INFORMATION

The program shown below is an example of a Basic program calling a C function that calls a Basic procedure. The program, as written, will loop indefinitely between the error-handling routine and the Basic subprogram that generates the error. If the RESUME statement is changed to RESUME NEXT, the program executes the next statement in the subprogram: PRINT "Return from ERROR".

(The documentation incorrectly states that "The Basic statement containing the call to the non-Basic code is the statement that RESUME would reexecute." According to this statement, the CALL CSUB statement would be executed; however, execution actually returns to the Basic subprogram where the error occurred, as described above.)

The following is a code example:

REM ** Here is the Basic program

DECLARE SUB jump ()
DECLARE SUB csub CDECL ()
CLS
ON ERROR GOTO errhand
FOR i = 1 TO 5
  PRINT " Before call to c ", FRE(-2)
  CALL csub
  PRINT " out if c:", FRE(-2)
NEXT
PRINT "At end ", FRE(-2)
END
errhand:
   PRINT " error handle => ", FRE(-2)
   RESUME
SUB jump
  print " Generate and Error"
  ERROR 9
  print "Return from ERROR "
END SUB
                
extern void fortran jump(void);

void csub(void)
 {
   jump();
 }
                


Additional query words: QuickBas BasicCom

Keywords: KB41390