Style question: returning error values from functions

Richard Gaskin ambassador at fourthworld.com
Sat Jul 12 07:20:01 EDT 2003


When crafting a function it's often useful to provide info about any errors
that occur within it.

Commands have a great mechanism for this:  we can put error info into the
result, and the calling handler can check the result just as it does for any
built-in command.

But functions don't have this luxury, as the result is the return value.

Two types of solutions that have become popular over the years:

- XFCN-style: Error info is returned directly from the function, and the
calling handler needs to check for it with something like:

  get foobar()
  if word 1 of it = "error" then
     answer line 2 of it
     exit to top
  end if

This approach has been popular with XFCN authors for years, but the problem
is that it's potentially ambiguous: what if the function works correctly but
the first word in the returned data happens to be "error"?


- Separate function:  Similar to how the sysError function works, you put
any error info in a global or static var with a separate function to access
it:

   get foobar()
   if fooErr() is not empty then
      answer fooErr()
      exit to top
   end if

This completely separates returned data from error info unambiguously, but
requires a little more effort to define functions reliably with it (it needs
to be cleared at the top of any function that uses it).


Which approach do you prefer?  One of these?  Something else?

-- 
 Richard Gaskin 
 Fourth World Media Corporation
 Developer of WebMerge 2.2: Publish any database on any site
 ___________________________________________________________
 Ambassador at FourthWorld.com       http://www.FourthWorld.com
 Tel: 323-225-3717                       AIM: FourthWorldInc




More information about the use-livecode mailing list