Reputation: 93
I want use try...catch
in Python but Python give me an error what's can I do?
try:
input('enter your number');
catch:
print('please give integer');
Upvotes: 3
Views: 9507
Reputation: 1200
try:
int(input('enter your number'));
except ValueError:
print('please give integer');
Upvotes: 5
Reputation: 610
You can use following alternative for your use case :
try:
input_ = int(input('enter your number'))
except:
print('please give integer')
exit() #if you want to exit if exception raised
#If no exception raised, execution continues from here
print(input_)
Upvotes: 1
Reputation: 608
try/catch in python is try/except.
you can use try/except like this:
try:
input('enter your number');
except expression:
print('please give integer');
Upvotes: 7