0

I'm very new to python so I have very simple doubt. Here is my code:

a=sri

try:
    print a
except Exception:
    print 'you have not put quotes for string'
else:
    print 'dont know what error it is'

How to write a manual exception/error handling for this?

1 Answer 1

1

The code for which you need to handle errors should be written in the try clause. The except clause is where you write how to handle the possible exceptions.

try:
    a=sri
    print a
except Exception:
    print 'you have not put quotes for string'

More on try statement from python docs.

The try statement works as follows.

  • First, the try clause (the statement(s) between the try and except keywords) is executed.
  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.
  • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
Sign up to request clarification or add additional context in comments.

2 Comments

please don't use blank exceptions. you can catch the example with NameError
Although using exception handling to catch errors you typed in your own code is kind of silly to begin with.

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.