Update main.cpp

adding error handling