! ERROR.BAS - testing out error trapping
! 1/11/82  SKE

map1 interesting'input,f
map1 error'code,f
map1 dummy,s,1
map1 choice,f

on error goto error'trap

menu:
       ? tab(-1,0)
       ? tab(10,10); "let's play kill the system. try to -"
       ? tab(12,5); "1. divide by zero"
       ? tab(13,5); "2. allocate a file that's 200,000 blocks long"
       ? tab(14,5); "3. open a file twice"
       ? tab(15,5); "4. read some nonexistant data"
       ? tab(16,5); "5. store data into PPN 999,999"
       ? tab(17,5); "6. quit (this function really works)"
       ? tab(21,5); "(Control C will be trapped out so you can try that too.)"
       ? tab(19,3);
       input "pick your poison (1-6) ==> ", choice
       on choice call zero, big'blocks, open'twice, no'data, no'ppn, quit
       goto menu

zero:
       ? tab(-1,0)
       ? tab(10,10); "this exercise divides by zero"
       ? tab(12,10);
       input "enter a number between 1 and 100 ==> "; interesting'input
       ? tab(14,10); interesting'input " divided by 0 = ";
       ? interesting'input / 0
       zero'plus'one:
       call pause
       return

big'blocks:
       ? tab(-1,0)
       ? tab(10,10); "Ever wonder what would happen if you tried to store"
       ? tab(11,10); "more information than the system would hold? Well,"
       ? tab(12,10); "we are about to allocate a file 200,000 blocks long"
       ? tab(13,10); "with the statement:"
       ? tab(14,10); "ALLOCATE ''NITWIT.DAT'', 200000"
       allocate "nitwit.dat", 200000
       big'blocks'plus'one:
       call pause
       return

open'twice:
       ? tab(-1,0)
       ? tab(10,10); "This little error hardly needs introduction. Well"
       ? tab(11,10); "known to all of us, it opens the same file twice."
       open #1, "nitwit", output
       open #1, "nitwit", output
       open'twice'plus'one:
       call pause
       return

no'data:
       ? tab(-1,0)
       ? tab(10,10); "This little etude tries to read information from"
       ? tab(11,10); "a data line that doesn't exist -- here goes --"
       ? tab(12,10); "read dummy"
       read dummy
       ? "dummy ="; dummy
       no'data'plus'one:
       call pause
       return

no'ppn:
       ? tab(-1,0)
       ? tab(10,10); "We will now try to open a file in PPN 999,999"
       ? tab(11,19); "with the statement: "
       ? tab(13,10); "OPEN #1, ''BOGUS.DAT[999,999]'', OUTPUT"
       open #1, "bogus.dat[999,999]", output
       no'ppn'plus'one:
       call pause
       return


quit:
       ? tab(-1,0)
       ? tab(22,1); "Better luck next time - end of system demolition derby"
       end

error'trap:
       if err(0) = 1 &
           then &
               ? tab(22,1); "DON'T DO THAT!" :&
               resume
       if err(0) = 10 &
           then &
               ? tab(22,1); "You just tried to divide by zero and "; :&
               ? "that's a no no!" :&
               resume zero'plus'one
       if err(0) = 19 &
           then &
               ? tab(21,1); "Not only did you run out of room on the disk"; :&
               ? " but there's a good chance "; :&
               ? "you've left the disk with no free blocks - you'd better"; :&
               ? "check it out!" :&
               resume big'blocks'plus'one
       if err(0) = 13 &
           then &
               ? tab(22,1); "Nice try, but that file is already open!" :&
               close #1 :&
               resume open'twice'plus'one
       if err(0) = 4 &
           then &
               ? tab(21,1); "you wouldn't try to feed a poor computer " :&
               ? "an empty data statement would you? " :&
               resume no'data'plus'one
       if err(0) = 16 &
           then &
               ? tab(22,1); "Sorry, that's not a legal PPN number" :&
               resume no'ppn'plus'one
       ! if err(0) isn't one of the above - we don't know what to do so...
       on error goto
       ! this turns AMOS error detetction & reporting back on

pause:
       ? tab(23,1);
       input "hit RETURN to continue ==> ", dummy
       return

! End of ERROR.BAS