move subprocess exception handler closer to the related try block