Obtaining the Module Name and Address of an Error

---------------------------------------------------------------------
The information in this article applies to:

- The Standard and Professional Editions of Microsoft Visual Basic for
  MS-DOS, version 1.0
--------------------------------------------------------------------

In a compiled program, if you use ON ERROR GOTO 0 in an error handler, 
Basic prints a message such as the following.

   Overflow in line 6 of module TEST at address 4D40:00B0

The offset (00B0 in this example) locates the ON ERROR GOTO 0
statement rather than the statement that caused the error. Therefore,
if you use ON ERROR GOTO 0, and you want to locate the statement that
caused an error based on the offset, you can use the technique
described below. Alternatively, if you used line numbers in your
program, you can locate the cause of the error by using the line number
shown in the error message (6 in this example).

The module ERR.OBJ contains the procedure ErrAddress which obtains the
module name and the address offset at which a trappable run-time error
occurred. To use ErrAddress, follow these steps.

1. Call ErrAddress from your error handler and print the module name
   and address offset returned.

   DECLARE SUB ErrAddress (modname$, SEG segment%, SEG offset%)
   ON ERROR GOTO errorHandler
   ...
   errorHandler:
   CALL ErrAddress(a$, segment%, offset%)
   PRINT "error in "; a$; " line:"; ERL; "offset "; HEX$(offset%)

2. Compile your modules using BC option /O and specify a listing file.
   For example,

   BC /O /E TEST.BAS,,TEST.LST

   The listing file shows the offset of each statement.

3. Link and run your program. When the error occurs, look up the
   offset printed by the error handler in the module's listing file.
   The error occurred in the code immediately previous to the address.

ErrAddress must be called directly from the error handler. It cannot
be called from a subroutine or procedure that was called from the
error handler. ErrAddress works with programs compiled with Visual
Basic for MS-DOS only.

Example Code
------------
The following example demonstrates how to use ErrAddress.

1. Place the following code in a module named TEST.BAS:
 
   DECLARE SUB ErrAddress (modname$, SEG segment%, SEG offset%)
   1 ON ERROR GOTO errorHandler
   2 ERROR 6  ' cause overflow error
   3 END
   errorHandler:
   4 CALL ErrAddress(a$, segment%, offset%)
   5 PRINT "error in "; a$; " line"; ERL; "offset "; HEX$(offset%)
   6 ON ERROR GOTO 0
 
2. Compile and link by using these commands:
 
   BC /O /E TEST.BAS,,TEST.LST;
   LINK TEST.OBJ ERR.OBJ;
 
3. Run TEST.EXE.

TEST.EXE prints the offset of the error (0043). In TEST.LST, this offset
appears just after the line that caused the error:
 
   Offset  Data    Source Line
   ...
   003A   0006    2 ERROR 6  ' cause overflow error
   0043   0006    3 END

