REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: attempt

attempt  block

Tries to evaluate a block and returns result or NONE on error.

Arguments:

block [block!]

See also:

try   error?  

Description

The attempt function is a shortcut for the frequent case of:

error? try [block]

More accurately, this is performed:

if not error? try [set/any 'val block] [val]

The format for attempt is:

attempt [block]

attempt is useful where you either do not care about the error result or you want to make simple types of decisions based on the error.

attempt [make-dir %fred]

attempt returns the result of the block if an error did not occur. If an error did occur, a none is returned.

In the line:

value: attempt [load %data]
probe value
none

the value is set to none if the %data file cannot be loaded (e.g. it is missing or contains an error). This allows you to write conditional code such as:

if not value: attempt [load %data] [print "Problem"]
Problem

Or code such as:

value: any [attempt [load %data] [12 34]]
probe value
[12 34]


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