refactoring code. Print error message or rethrow exception when error
occurs.