Using 'try'

David Burgun dburgun at dsl.pipex.com
Thu Apr 13 05:01:38 EDT 2006


Hi,

A try statement will "catch" an occurrence of a "throw". The throw  
can be performed anywhere in the engine or from one of your own  
handlers/functions. If an "exception" is "'thrown" and there is no  
"try" structure in place, then an execution error will be generated  
(e.g. the Engine or IDE catches the error). In the case below, Mark  
didn't care what the exception actually was, he just wanted it to be  
caught so as not to cause an execution error or corrupt or the  
"forecolor". Just using the try without the corresponding catch is  
allowed and is just shorthand for:

try
do something
catch myError
end try

You can use try/catch and throw in your own scripts to make it easier  
to handle errors or make it easier to debug. Say you had a number of  
functions that all call each other and any one of them can generate  
an error, there are two main ways of handling this:

1.  Returning Error Codes.

on mouseUP
put Func1() into myErrorCode
if myErrorCode != 0 then
   answer "ErrorCode:" && myErrorCode
   exit mouseUp
end if

function Func1
if <something isn't right> then return 1

put Func2() into myErrorCode
if myErrorCode != 0 then return myErrorCode

return 0
end Func1

function Func2
if <something isn't right> then return 2

put Func3() into myErrorCode
if myErrorCode != 0 then return myErrorCode

return 0
end Func2

function Func3
if <something isn't right> then return "3"

return 0
end Func3


2 Using Throw and Catch.

on mouseUP
try
    get Func1()

catch myErrorCode
   answer "ErrorCode:" && myErrorCode
   exit mouseUp
end try
end mouseUp

function Func1
if <something isn't right> then throw 1

get Func2() myErrorCode

return 0
end Func1

function Func2
if <something isn't right> then throw 2

get Func3()

return 0
end Func2

function Func3
if <something isn't right> then throw 3

return 0
end Func3

Hope this Helps
All the Best
Dave

On 13 Apr 2006, at 08:28, Graham Samuel wrote:

> In the very interesting discussion on Custom Properties, Mark  
> Wieder introduced this example
>
>> setProp NewValue pValue
>>   try
>>     if pValue < 0 then
>>       set the foreColor of me to "red"
>>     else
>>       set the foreColor of me to empty
>>     end if
>>   end try
>>   put pValue into me
>> end NewValue
>
> I think I get the idea, but Mark, why did you use 'try'? It would  
> not occur to me to do this - indeed I don't think I've ever used  
> 'try', which if I understand correctly, allows the script to  
> capture an error rather than handing it to the RR engine: but that  
> involves 'catch' doesn't it? Is there any advantage to using 'try'  
> the way you did?
>
> Just curious - there's so much I don't know!
>
> Graham
>
>
> ---------------------------------------------------------------------- 
> -----
> Graham Samuel / The Living Fossil Co. / UK and France
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list