RE: How to handle failed script
This is a very subjective question here, you can handle script failure in lot of ways. I cannot give you code for this as it is very specific to what you want to achieve.
– Usually your script failure handler is either event driven or is a part of the Test runner.
– You can handle the failure by reporting it first and then marking it for a rerun.
– If you are using testng as your framework, you may want to tap in the ITestListener interface of TestNg. It has two methods, OntestFaliure and OnTestSkipped. You can write the code to take action son failure and skip in these methods.