[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applesoft/Prodos/ONERR GOTO/File existence question



(Note: I have not looked at other responses yet)

Eric Smith (eric-no-spam-for-me@brouhaha.com) wrote:
: Hans Franke and I submitted an entry to the Retrocode competition at
: VCF 6.0 today, and came in second.  I had a bit of a problem in one
: area, and thought maybe one of you experts out there might offer a
: suggestion.

: The code looked vaguely like this:

: 10 DIM M%(38,38)
: 20 GOSUB 10000: REM Load or generate maze

: ...

: 10000 GOSUB 20000: REM Try to load maze from data file
: 10010 IF M%(0,0)=15 THEN RETURN: REM Loaded maze
: 10020 REM Generate a new maze
: ...
: 10500 REM Write maze to file
: 10510 PRINT CHR$(4);"OPEN MAZE.DAT"
: 10520 PRINT CHR$(4);"WRITE MAZE.DAT"
: ... nested loops with PRINT statements
: 10580 PRINT CHR$(4);"CLOSE MAZE.DAT"
: 10590 RETURN

: ...

: 20000 REM Try to read maze from file
: 20010 ONERR GOTO 20040: REM In case the file doesn't exist
: 20020 PRINT CHR$(4);"OPEN MAZE.DAT"
: 20030 PRINT CHR$(4);"READ MAZE.DAT"
: ... nested loops with INPUT statements
: 20040 PRINT CHR$(4);"CLOSE MAZE.DAT"
: 20050 POKE 216,0: REM Cancel ONERR
: 20060 RETURN

Wait a minute...
With this code, you should NEVER fall out with a FILE NOT FOUND.  When you
OPEN the file, if it doesn't exist, it will be created for you...then you
will fall out of the loop with an END OF DATA error instead.

: I'm not claiming that this is particularly good style.  We only had 3 hours
: to write the whole contest entry, and most of the time was spent on the game
: algorithms and debugging.

: Anyhow, the problem was that any time the program was run without a
: preexisting MAZE.DAT file, it would get the error and trap to line 20050,
: but when it got to the RETURN statement in 20060, it would then get a
: RETURN WITHOUT GOSUB error.

: So I have two questions:

: 1)  Does an error trapped using ONERR really lose the subroutine stack?
:     Or do I need to do something else to make the RETURN work?  Note that
:     I definitely do NOT want to RESUME the statement that caused the
:     error.

Hmmm...The Applesoft Tutorial says that, "After an error occurs, Applesoft
BASIC returns to command level as indicated by the ] prompt character and
a blinking cursor.  Variable values and the program text remain intact,
but the program cannot be CONTinued and all GOSUB and FOR loop counters
are set to 0."  It isn't clear whether that reset happens when Applesoft
catches the error, before the ONERR trap, or after the program crashes.