Catch Multiple Exceptions in One Line in python

Catch Multiple Exceptions in One Line in python
(Last Updated On: 10/05/2023)

Python Program to Catch Multiple Exceptions in One Line

# Python Program to Catch Multiple Exceptions in One Line

# variable declaration
a = 1
str = "hello"


# function definition
def func(a):
	result: a + str # unboundlocalerror
	print(result)

# try block to get the exception
try:
	func(a)
except(TypeError, UnboundLocalError) as e:
	print(e)

Output:

local variable 'result' referenced before assignment

Leave a Reply

Your email address will not be published. Required fields are marked *