How to get line number of exception in Python?
The output of the following code
try:
print("a" + 1)
except Exception as error_message:
print("There was an error: " + str(error_message))
is
There was an error: can only concatenate str (not "int") to str
But instead of just printing the
"There was an error: " + str(error_message)
how to print the line number as well like this example
try:
print("a" + 1)
except Exception as error_message and linenumber as linenumber:
print("There was an error: " + str(error_message) + ". The line where the code failed was " + str(linenumber))
with the expected output of
There was an error: can only concatenate str (not "int") to str. The line where the code failed was 2
This would be really useful for me when debugging my projects