home *** CD-ROM | disk | FTP | other *** search
- \ Forth source file
- \ Copyright Mads Meisner-Jensen, 6 Apr 1993
- \ Example of THROW and CATCH and general error handling
-
- : DIVISION-FAULT ( u -- )
- 5 -5 DO
- CR \ newline
- DUP . \ print dividend
- ." / " \ print slash with trailing space
- I . \ print divisor
- ." = " \ print equals sign
- DUP I / . \ make division and print result
- LOOP \ loop
- DROP \ drop dividend
- ;
-
- : MAIN ( -- )
- 100 \ parameter for DIVISION-FAULT
- ['] DIVISION-FAULT CATCH \ call word DIVISION-FAULT and install
- \ error interception frame.
- IF \ if an error occurred then ..
- DROP \ drop the parameter(s) passed to DIVISION-FAULT
- ." word DIVISION-FAULT failed."
- THEN
- ;
-
-
- \ Now try typing MAIN and see how MAIN catches the error produced by
- \ DIVISION-FAULT.
- \ CATCH can actually choose to THROW the value again, passing the THROW
- \ to a CATCH at a higher level.
- \ CATCH of course also works if the word CATCH calls, calls another word
- \ which calls another word etc. which eventually THROWs (up?).
-