code for when error occurs do something

atlas_1:
I wish to write a code that does something when there is an error in my coding.

If you could detect an error in your coding then you would know what the error was and you wouldn't put it in your code in the first place.

If you want to detect an error in the sensor then that is dependent on the sensor. If there is a way to determine the sensor is giving readings that are out of bounds then you can do whatever you wish when you detect the error. In production systems the response to an error depends on the severity of it and the potential consequences the error can have on the system.