3

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');
3
  • 3
    use except instead of catch Commented Sep 21, 2018 at 10:42
  • Try here Commented Sep 21, 2018 at 10:43
  • 1
    except is mostly explained using the words "catch exceptions" in the docs and hence I guess you used the word catch ;) Commented Sep 21, 2018 at 10:43

3 Answers 3

7

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');
Sign up to request clarification or add additional context in comments.

2 Comments

What is the expression here?
nice semi comma use there :)
5
try: 
    int(input('enter your number')); 
except ValueError: 
    print('please give integer');

Use handling-exceptions

Comments

1

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_)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.