There has to be at least one except statement in a try-except block.
The try-except block will help in handling selected exceptions.
try statement works as follows.
- First, the try clause (the statement(s) between the
exceptkeywords) is executed.
- If no exception occurs, the except clause is skipped and execution of the
trystatement is finished.
- If an exception occurs during the execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the
exceptkeyword, the except clause is executed, and then execution continues after the
- If an exception occurs which does not match the exception named in the except clause, it is passed on to outer
trystatements; if no handler is found, it is an unhandled exception, and execution stops with a message as shown above.
try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed.