Implemented Error handling routine

The program will not get stuck now.