Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: catch in a function - what does it do?

From: rebol:techscribe at: 23-Jan-2001 1:28

Hi Anton, let me add a few words. Normally when an error occurs, you get an error message similar to this:
>> f: func [] [ try [ 1 / 0 ] ] >> f
** Math Error: Attempt to divide by zero. ** Where: 1 / 0 That's ok in our case. But as soon as we have more complicated call sequences, where one function may call several other functions that in turn call yet other functions:
>> g: does [ f ] >> g
** Math Error: Attempt to divide by zero. ** Where: 1 / 0 If this happens during the evaluation of a long script, it'll take some time to determine where this error occurs. The catch attribute works together with the throw native and modifies where the error is reported:
>> f: func [ [catch] ] [ throw try [ 1 / 0 ] ] >> g
** Math Error: Attempt to divide by zero. ** Where: f Even though we called the g function and the error occured in the try block of the f function, the error is no longer reported as having occurred ** Where: 1 / 0 instead we get ** Where: f which helps us more easily locate the source of the problem. In our case the error occurs in the f function, and the error message reports that that is where the error occurred. Another example. Let's say many function call the f function, but f fails only sometimes. Now we are no longer interested in the fact that the error in occurs in f, we want to know which of the calling functdions is causing the error. So we remove [catch] from f, and instead use a catch in g to report the calling function:
>> g: func [ [catch] ] [ f ] >> f: does [ try [1 / 0] ] >> g
** Math Error: Attempt to divide by zero. ** Where: g Now g is reported as the location at which the error occured, because we have migrated catch attribute to g. In short, the catch-throw combo allows us to conveniently locate errors. Hope this helps, Elan Andrew Martin wrote: