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

catch in a function - what does it do?

 [1/6] from: arolls:bigpond:au at: 23-Jan-2001 16:27


Hi, Can someone explain what it means when 'catch is found in a function header? Regards, Anton.

 [2/6] from: gjones05:mail:orion at: 22-Jan-2001 23:57


Anton, The "REBOL/Core User's Guide" may answer this question for you starting on page 268. This document is available in pdf format online at: http://www.rebol.com/download_manual.html or the print version is available through www.rebolpress.com . Cheers to the down-under crowd. --Scott

 [3/6] from: al:bri:xtra at: 23-Jan-2001 20:54


Anton wrote:
> Can someone explain what it means when 'catch is found in a function
header? It's intended to catch errors that occur in the function (usually due to bad content in arguements and present them as if they occured out side the function. Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/

 [4/6] from: al:bri:xtra at: 24-Jan-2001 16:28


Elan wrote:
> let me add a few words.
Me, too! :-) When writing and designing a function, leave the [catch] attribute out until you're certain that you've finished debugging the function. When you're happy with it, then insert the [catch]. Why? It's really hard to find a bug in your function with the catch in it because you've told Rebol to not show the error location with the [catch] attribute! I hope that helps! Andrew Martin Getting fingers caught in Rebol catch... ICQ: 26227169 http://members.nbci.com/AndrewMartin/

 [5/6] from: g:santilli:tiscalinet:it at: 24-Jan-2001 20:29


Hello Andrew! On 24-Gen-01, you wrote: AM> insert the [catch]. Why? It's really hard to find a bug in AM> your function with the catch in it because you've told Rebol AM> to not show the error location with the [catch] attribute! Actually, only errors thrown with THROW will be catched by the [catch] attribute, AFAIK. Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [6/6] 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: