Using "Error opening URL" as a variable

I have created my own dynamic photo gallery.

I have a loadmovie command with a url like this:

photo =“http://www.domain.com/data_base/gallery/"+photoNum+".jpg”;
screen.loadMovie(photo);

As you can see I have a variable in my url called photoNum. This is a number and it increases by one as I hit a next button in my flash movie.

on (release) {
screen.loadMovie(photo);
photoNum = photoNum+1;
}

All of this works as it should. My problem is when I have 30 photos in my gallery and I am viewing the last photo, photoNum = 30. I hit my next button and photoNum = 31, I get a blank screen. I am looking for a way to go back to the first photo when this happens, photoNum = 1. Can I take the error message that flash outputs and use it as a variable? Something like:

if(Error opening URL==true){
photoNum =1
}

I have some other ways that will probable working using things like onData and a timer. If onData is not invoked after 5 sec, photoNum = 1. This way is to slow. Can my idea work or is there a better way altogether?