Define function for error handling.