factor/library/errors.factor

44 lines
1.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-03-21 14:39:46 -05:00
IN: errors
USING: kernel kernel-internals lists sequences ;
2005-08-25 15:27:38 -04:00
! This is a very lightweight exception handling system.
2004-07-17 18:35:09 -04:00
2005-08-25 15:27:38 -04:00
TUPLE: no-method object generic ;
2005-09-17 22:25:18 -04:00
: no-method ( object generic -- ) <no-method> throw ;
2004-11-25 21:51:47 -05:00
2004-11-25 23:14:17 -05:00
: >c ( catch -- ) catchstack cons set-catchstack ;
: c> ( catch -- ) catchstack uncons set-catchstack ;
2004-07-17 18:35:09 -04:00
: catch ( try -- exception/f | try: -- )
#! Call the try quotation. If an exception is thrown in the
#! dynamic extent of the quotation, restore the datastack
#! and push the exception. Otherwise, the data stack is
#! not restored, and f is pushed.
2005-09-18 23:22:58 -04:00
[ >c call f c> drop f ] callcc1 nip ; inline
2005-09-14 00:37:50 -04:00
2004-07-17 18:35:09 -04:00
: rethrow ( error -- )
#! Use rethrow when passing an error on from a catch block.
catchstack empty?
[ die "Can't happen" throw ] [ c> continue-with ] ifte ;
: cleanup ( try cleanup -- | try: -- | cleanup: -- )
#! Call the try quotation. If an exception is thrown in the
#! dynamic extent of the quotation, restore the datastack
#! and run the cleanup quotation. Then throw the error to
#! the next outermost catch handler.
[ >c >r call c> drop r> call ]
2005-09-22 23:18:12 -04:00
[ drop (continue-with) >r nip call r> rethrow ] ifcc ; inline
2005-09-21 01:12:16 -04:00
2005-09-22 16:21:36 -04:00
: recover ( try recovery -- | try: -- | recovery: error -- )
2005-09-21 01:12:16 -04:00
#! Call the try quotation. If an exception is thrown in the
#! dynamic extent of the quotation, restore the datastack,
#! push the exception on the datastack, and call the
#! recovery quotation.
[ >c drop call c> drop ]
2005-09-22 23:18:12 -04:00
[ drop (continue-with) rot drop swap call ] ifcc ; inline
2005-03-25 21:43:06 -05:00
GENERIC: error. ( error -- )