Style question: returning error values from functions

Dar Scott dsc at swcp.com
Sat Jul 12 17:56:01 EDT 2003


On Saturday, July 12, 2003, at 03:21 PM, erik hansen wrote:

> --- Dar Scott <dsc at swcp.com> wrote:
>>> I readily use throw in scripts for my own
>> use. Are people comfortable with throw?
>
> what is "throw"?
> it sounds exciting.

I think in Revolution it is something like this:

A throw allows you to create an error much like sqrt(-1) does.  You can  
use it in custom command handlers and in custom functions.

Example:

function areaOfRectangle w, h
   if (w<=0) or (h<=0) then throw "Bad Rectangle Dimensions"
   return w*h
end areaOfRectangle

This example applies directly to the original question.

(I don't know of anything in Revolution that requires that throws be  
used only for errors, but that is normally the case.)

What makes 'throw' exciting is the use of 'try', or try-catch as it is  
sometimes called.  Both errors reported with 'throw' in your scripts  
and errors reported with a built-in throw in built-in commands and  
scripts can be contained with 'try'.

If there is an error and it is not caught with a try catch (or, if so,  
it is re-thrown) then it is handled by a custom handler or by the  
system.

You can use 'try' like this:

try
    put fastExperimentalComputation(x,y,z) into w
catch errVal
    put triedAndTrueComputation(x,y,z) into w
end try

If there is an error anywhere in fastExperimentalComputation() and it  
is not caught at an intermediate level, then its execution is stopped  
and execution continues in the catch clause.  If there is no error, the  
catch clause is not executed.  The error can be many functions and  
handlers down in calls.  (A 'try' might not do any good for a bug in  
the engine.)

The optional 'finally' clause is executed however control gets out of  
the try.  Use it like this:

local veryVeryLargeTempArray
try
   build veryVeryLargeTempArray
   doSomethingWith veryVeryLargeTempArray
catch errVal
   throw errVal
finally
   put empty into veryVeryLargeTempArray
end try

Or like this:

try
   controlMotor(a,b,x,y,t)
catch errVal
   if errVal is not "motor control error" then throw errVal
   put true into motorError
finally
    pullThePlug
end try

This will shut down the motor under all error conditions as well as all  
normal conditions.  I'm not sure, but I expect it will even if--in some  
fit of idiocy--you put an 'exit to top' command in one of the  
controlMotor routines.

You can use 'try' as one of your methods to make your applications  
robust.  Even if it trips, your app can land on its feet and casually  
go on.

Look at the TD entries for try and throw.  There are also a couple  
simple examples.

Dar
************************************************************************ 
****
   Dar Scott Consulting    http://www.swcp.com/dsc/    Programming  
Services
************************************************************************ 
****




More information about the use-livecode mailing list