Add a trap to catch any problem when exit.