Modify script to handle exception