Handling an error in a repeating loop

I’ve got a complicated script for copying movies from SD cards. I just boiled down the script to something below so I can demonstrate the issue. Basically, I want the user to tell me how many camera cards they have, then proceed with a loop for each of those camera cards. Now, my big problem is what if the user makes a mistake 5 cards into a 10 card ingest? Is there a way I can restart that instance of the loop instead of going to the next number in the count? Also, is there a way that, if the user clicks cancel, it just cancels that loop and not the whole script?

set NumberofCards to text returned of (display dialog "How Many Cards do you have?" default answer "1")
with timeout of (30 * 60) seconds
	repeat with i from 1 to NumberofCards
		tell application "System Events"
			display dialog ("Insert card " & i & " of " & NumberofCards as text) & " into the reader."
			set these_items to choose folder with prompt "SELECT YOUR CAMERA CARD!"
			set showtitle to (choose from list {"Show1", "Show2", "Show3"} with prompt "What is the Show Title?")'s item 1
			set shortdescription to (display dialog "Short Description")'s text returned
			set dateofclip to (display dialog "Date")'s text returned
			set cameraletter to (display dialog "Camera Letter and Reel Number?")'s text returned
			set category to (choose from list {"_SHOOT", "_Interview"} with prompt "Category")'s item 1
			set cardName to showtitle & "_" & shortdescription & "_" & dateofclip & "_" & cameraletter & category
			if (count (cardName)) is greater than 59 then display dialog "ERROR: Name must be 59 characters or less" buttons {"I'll try again"} default button 1 cancel button 1
			if button returned of (display dialog cardName with title "Clip Name:" buttons {"Let me try again", "Enter Myself", "Proceed"} default button 3 cancel button 1) is "Enter Myself" then set cardName to (display dialog "Enter File Name Yourself Showtitle_ShortDescription_Date_Camera_Category" default answer "Enter Here")'s text returned
			set cardBackupFolder to (make new folder at (myVolume & ":VirtualAE:CardBackups" as string) with properties {name:cardName & "_CardBackup"})
			duplicate these_items to cardBackupFolder
                        display dialog "Eject your card"
		end tell
	end repeat
end timeout
tell application "System Events"
	display dialog "You have finished ingesting your " & NumberofCards & " cards."
end tell

Here’s a bit of pseudocode for one solution:

set canContinue to false

repeat until canContinue is true
	# get user input here
	try
		# test input here; raise error on fail, and the boolean will not be set
		# if OK, set the flag, and repeat loop will be exited
		set canContinue to true
	end try
end repeat

This is a working example to get a natural number from the user:

set canContinue to false

repeat until canContinue is true
	set steps to text returned of (display dialog myMsg default answer "" buttons {"OK"} default button "OK")
	try
		if (steps as integer < 1) or (steps contains ".") or (steps contains ",") or ((count words of steps) > 1) then
			display alert "Not a whole number" message "Only a positive integer is accepted." buttons {"OK"} default button "OK" as warning
			error # MUST have this statement for it to work
		end if
		set steps to steps as integer
		set canContinue to true
	end try
end repeat

You need to do this for each input where mistakes are likely.
Or just one loop containing all inputs, with ‘redo’ buttons to signal a mistake.

Perfect, I was able to make that work by nesting a bunch of these “canContinue” type variables in each other so that you can go back at any point in the multi-step process. Thank you!

You can use a double repeat, like this:

repeat with i from 1 to NumberofCards
 repeat 1 times
  -- do your stuff here
  try
    -- something that might error
  on error
   exit repeat -- exits inner repeat
  end try
  -- more stuff here
 end repeat
end repeat