Catch errors when replacing variables