In a very well written and awesome book by Robert Smallshire and Austin Bingham there is the following code:
def convert(s):
"""Convert a string to an integer."""
try:
return int(s)
except (ValueError, TypeError) as e:
print("Conversion error: {}".format(str(e)), file=sys.stderr)
raise
after that, the author has included the following string_log function
def string_log(s):
v = convert(s)
return log(v)
and the output of string_log('ouch')
is as follow
I am not able to understand what raise statement exactly works?
Also, the author says that the code where the first function outputs negative error code of -1 in case of ValueError
and TypeError
i.e the first function would be as follow
import sys
def convert(s):
"""Convert a string to an integer."""
try:
return int(s)
except (ValueError, TypeError) as e:
print("Conversion error: {}".format(str(e)), file=sys.stderr)
return -1
and than if we run the following (without any change made string_log()
) is
string_log('cat')
Is unpythonic but why?