Except exception in python

Share:

Except exception in python

What is except exception in python?

In the try clause, all statements are executed until an exception is encountered. except is used to catch and handle the exception(s) that are encountered in the try clause. else lets you code sections that should run only when no exceptions are encountered in the try clause.


code :

try:

    num1 = int(input("Your Value"))

    num2 = int(input("Your 2nd Value"))

    print("sum of these is",num1+num2)

except Exception as a:

    print("Note: your program should be error-free")

Output :

Your Value1

Your 2nd Value2

the sum of these is 3

or

Your Value 4

Your 2nd Value Najwa

Note: your program should be error-free