Yeah, as Bobbel suggested you can check if the image exists before you try to use it. Do it right and you won't have to do an emergency save before it quits prematurely because it won't quit when you don't want it to.
` Make random number picking more random
randomize timer()
` Create 20 images
for t=1 to 20
` Pick a random image number between 1 and 1000
i=rnd(999)+1
` Make an image
ink rgb(rnd(255),rnd(255),rnd(255)),0
box 0,0,50,50
ink rgb(255,255,255),0
center text 25,17,str$(i)
` Grab the image
get image i,0,0,50,50,1
next t
` Show the images (go through every possible image number it could be)
for t=1 to 1000
` Check if the image number exists
if image exist(t)
` Show the image
paste image t,rnd(640),rnd(480),1
endif
next t
wait key