alexsan
alexsan

Reputation: 93

How can use Try/Catch in Python

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

Answers (3)

Umesh Yadav
Umesh Yadav

Reputation: 1200

try: 
    int(input('enter your number')); 
except ValueError: 
    print('please give integer');

Use handling-exceptions

Upvotes: 5

awakened_iota
awakened_iota

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

Abdi
Abdi

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

Related Questions