Re: About GOTO (was RE: fixed windows)
- Posted by David Cuny <dcuny at LANSET.COM> Aug 07, 2003
- 401 views
I've implemented REDO, BREAK and CONTINUE in my own BASIC interpreter. Additionally, there is a ELSE clause on all the control structures, so if they complete without hitting a BREAK clause, the THEN executes: for i = 1 to length( s ) if s[i] = 999 then break end if else printf( "999 was not found" ) end for It saves having to set up a flag marking that something was found, and is cheap to implement. What none of these provides is the ability to jump out an arbitrary number of levels - for example: for i = 1 to 10 while a < length( s ) for j = 1 to 10 for k = 1 to 10 -- LEAVE HERE... end for end for end while -- ... AND JUMP TO HERE end for For this, TRY/CATCH is probably the best thing: integer MyError = NEW_ERROR( "My custom error" ) for i = 1 to 10 TRY while a < length( s ) for j = 1 to 10 for k = 1 to 10 THROW MyError end for end for end while CATCH { MyError } END TRY end for Another option would be to implement a more limited version of TRY/CATCH, like this: for i = 1 to 10 while a < length( s ) for j = 1 to 10 for k = 1 to 10 THROW MyError end for end for CATCH { MyError } end while end for Note the CATCH clause is attached to the WHILE loop. THROW would cause a continuous BREAK until a CATCH clause is found. The problem with this is that it gives you less granularity than if you had a TRY control structure. Also, TRY has a cool FINALLY clause, which guarantees to always execute, whether or not an error was thrown, even if a RETURN is encountered in the clause. This helps ensure that resources that were taken are always disposed of. -- David Cuny