Single-pass Loop for Handling Errors in FileMaker

Thanks to Dawn Heady for this nifty idea

A simple routine for handling an error in a FileMaker script might look like this:

Set Error Capture [On]
#
#Do some stuff
#
Set Variable [$error; Value:Get ( LastError )]

If [$error]
   #Cleanup
   Set Field [Globals::Field; ""]
   Go to Layout [original layout]
   Show Custom Dialog ["Error"; "Oops! Something happened."]
   Exit Script []
End If

A script with more steps and more potential errors might grow to look like this. Note that our “cleanup” steps have to be repeated with each error:

Set Error Capture [On]
#
#Do some stuff
#
Set Variable [$error; Value:Get ( LastError )]
If [$error]
   #Cleanup
   Set Field [Globals::Field; ""]
   Go to Layout [original layout]
   Show Custom Dialog ["Error"; "Oops! Something happened."]
   Exit Script []
End If
#
#Do some more stuff
#
Set Variable [$error; Value:Get ( LastError )]
If [$error]
   #Cleanup
   Set Field [Globals::Field; ""]
   Go to Layout [original layout]
   Show Custom Dialog ["Error"; "Oops! Something happened."]
   Exit Script []
End If
#
#Do some more stuff
#
Set Variable [$error; Value:Get ( LastError )]
If [$error]
   #Cleanup
   Set Field [Globals::Field; ""]
   Go to Layout [original layout]
   Show Custom Dialog ["Error"; "Oops! Something happened."]
   Exit Script []
End If

This is redundant and troublesome to maintain because we have to repeat the cleanup steps for each potential error. We could move each error-prone step to a separate script and return the result using Exit Script []. Or, we could put the whole process in a loop, exit the loop if there’s an error, and then handle any errors when we’re done:

Set Error Capture [On]
Set Variable [$error; 0]
#
#Single-pass loop to consolidate error handling
Loop
   #Do some stuff
   #
   Set Variable [$error; Value:Get ( LastError )]
   Exit Loop If [$error]
   #
   #Do some more stuff
   #
   Set Variable [$error; Value:Get ( LastError )]
   Exit Loop If [$error]
   #
   #Do some more stuff
   #
   Set Variable [$error; Value:Get ( LastError )]
   #
   Exit Loop If [1]
End Loop
#
#
If [$error]
   #Cleanup
   Set Field [Globals::Field; ""]
   Go to Layout [original layout]
   Show Custom Dialog ["Error"; "Oops! Something happened."]
   Exit Script [$error]
End If
#
Exit Script []
#

This way we only need to specify our cleanup steps once! If certain errors need to be handled separately, we could branch the If statement at the end accordingly.

This technique is a handy way to consolidate error handling, keeping code tidy and easy to maintain.

7 thoughts on “Single-pass Loop for Handling Errors in FileMaker”

  1. Hi, Christos,

    I'm responding to your question since Brent is no longer with Soliant. Yes, I use this design pattern often. It allows me to consolidate the error processing in one place within the script.

  2. Thanks for this article, I’ve been using this for a while and wanted to share this. Y’all probably already do it this way but figured I’d add it for anyone else.

    We can reduce the code a bit more if we change the ELI to…

    Exit loop if [ get(lastError) ]

    and for the trap at the bottom…

    if [ get(lastError) ]
    show custom dialogue
    #clean up
    End if

    OR

    If ( get (lastError) )
    set variable $error = get (lastError)
    #clean up
    Custom dialogue
    End If

    Super small difference, but they add up! Thank you again for this excellent technique!

  3. It would seem that, IF you are say, validating a number of field contents, that concatenating the list of fields and data, to be presented all at once, might be better than hit and repeat.

Leave a Comment

Your email address will not be published. Required fields are marked *

Are You Using FileMaker to Its Full Potential?

Claris FileMaker 2023 logo
Scroll to Top