home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / a293_1 / !AForth_Examples_Errors < prev    next >
Encoding:
Text File  |  1993-04-13  |  1018 b   |  34 lines

  1. \ Forth source file
  2. \ Copyright Mads Meisner-Jensen, 6 Apr 1993
  3. \ Example of THROW and CATCH and general error handling
  4.  
  5. : DIVISION-FAULT  ( u -- )
  6.   5 -5 DO
  7.     CR        \ newline
  8.     DUP .      \ print dividend
  9.     ." / "    \ print slash with trailing space
  10.     I .        \ print divisor
  11.     ." = "    \ print equals sign
  12.     DUP I / .    \ make division and print result
  13.   LOOP        \ loop
  14.   DROP        \ drop dividend
  15. ;
  16.  
  17. : MAIN  ( -- )
  18.   100          \ parameter for DIVISION-FAULT
  19.   ['] DIVISION-FAULT CATCH    \ call word DIVISION-FAULT and install
  20.                 \ error interception frame.
  21.   IF         \ if an error occurred then ..
  22.     DROP    \ drop the parameter(s) passed to DIVISION-FAULT
  23.     ." word DIVISION-FAULT failed."
  24.   THEN
  25. ;
  26.  
  27.  
  28. \ Now try typing MAIN and see how MAIN catches the error produced by
  29. \ DIVISION-FAULT.
  30. \ CATCH can actually choose to THROW the value again, passing the THROW
  31. \ to a CATCH at a higher level.
  32. \ CATCH of course also works if the word CATCH calls, calls another word
  33. \ which calls another word etc. which eventually THROWs (up?).
  34.