try-except Block


☞ When we use try…except block in the program and an error is encountered, a try block code execution is stopped and transferred down to the except block.

Syntax :

try:
    #run this code
except:
    #execute this code when there is an exception

Example :

try:
    a = int(input("Enter first number : "))
    b = int(input("Enter second number : "))
    c = a/b
    print("Answer = ", c)
except:
    print("Sorry!! you have done something wrong")

Output :

Enter first number : 5
Enter second number : 0
Sorry!! you have done something wrong