Catch IOException if script already ends with "exit"

Also reformatted a few comments around the affected code block