Add exception handling for TypeError