I would think that you can probably just do something like this (in Perl):
and BAIL_OUT("Unable to open URL");
The above will first try to open the page defined by $homepage and if it fails
will capture a screenshot AND bail out printing "Unable to open
This was an interesting question asked to me by a colleague and, as well, we have answered it in user groups a couple of times so I figured it would be worth noting here.