Improve the error handling logic and print appropriate output messages (on exceptions)