I'm trying to make a simple program (for learning purposes) using exception handling. What I'm trying to do is:
try:
x = int(input())
except ValueError as var:
# HERE I WANT TO USE THE 'x' STRING VALUE
I know about the the various ways to use the exception message (for instance by using str(var)).
For example, if the input is bla which would cause a ValueError exception, print(var) outputs invalid literal for int() with base 10: 'bla', which is not really usable.
However, I created this program that should use str(var) to my advantage:
try:
x = int(input())
except ValueError as var:
s = str(var)
print('Exception message: ' +s)
i=0
while (s[i] != '\''):
i = i+1
i = i+1
print_str = ''
while (s[i] != '\''):
print_str = print_str + str(s[i])
i++
print('print_str = ' + print_str)
This program would probably work after a few changes..
So my question is: Is there a more direct way to get the 'x' string value?
i++is invalid syntax. It has nothing to do with exception handling.