Try/Catch Statements in Python
In Python, try and except keywords are used. A try block is specified using the try keyword, and the body of the try statement is denoted by an indentation. Its body can contain one or several statements which need to be evaluated if an exception does not occur. The except block is called if the try block catches any errors. Instead of halting the program, the statements in its body are executed, and the program flow will not break.
Let's consider an example. Suppose we try to print a variable x without declaring it. This will cause the program to raise an exception. But we do not want the program to stop, hence we use a try-except block to work around this problem.
xxxxxxxxxxtry: print(x)except: print("There is no variable x")OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment


