Printing out actual error message for ValueError Printing out actual error message for ValueError python-3.x python-3.x

Printing out actual error message for ValueError


try:    ...except ValueError as e:    print(e)


Python 3 requires casting the exception to string before printing:

try:    ...except ValueError as error:    print(str(error))


Another approach using logging

import loggingtry:    int("dog")except Exception as e:    logging.warning(e)    logging.error(e)

gives

WARNING:root:invalid literal for int() with base 10: 'dog'ERROR:root:invalid literal for int() with base 10: 'dog'[Program finished]

Just typing the exception gives,

invalid literal for int() with base 10: 'dog'[Program finished]

Depends on how you want to process the output