REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Expressions: Trying Blocks

Pending Revision

This document was written for R2 and has yet to be revised for R3.

There are times when you want to evaluate a block, but should an error occur, you do not want to stop the evaluation of the rest of your script.

For example, you might be performing a number division, but do not want your script to stop if a divide-by-zero occurs.

The try function allows you to catch errors during the evaluation of a block. It is almost identical to do. The try function will normally return the result of the block; however, if an error occurs, it will return an error value instead.

In the following example, when the divide by zero occurs, the script will pass an error back to the try function, and evaluation will continue from that point.

for num 5 0 -1 [
    if error? try [print 10 / num] [print "error"]
]
2
2.5
3.33333333333333
5
10
error

More about error handling can be found in the errors Appendix.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin