Python Program to Catch Multiple Exceptions in One Line

string = input()


try:

    num = int(input())

    print(string+num)

except (TypeError, ValueError) as e:

    print(e)

Output

can only concatenate str (not "int") to str

In the above example, string and an integer cannot be added, so TypeError is caught.

Input

a
b

Output

invalid literal for int() with base 10: 'b'

In the above example, the second input should have been an integer, but we passed a string 'b'.

0 Comments