Skip to content

Instantly share code, notes, and snippets.

@justinmklam
Last active August 12, 2020 17:31
Show Gist options
  • Save justinmklam/5f9016be892808690f7c950e836ba266 to your computer and use it in GitHub Desktop.
Save justinmklam/5f9016be892808690f7c950e836ba266 to your computer and use it in GitHub Desktop.
def exception_handler(func):
def inner_function(*args, **kwargs):
try:
func(*args, **kwargs)
except TypeError:
print(f"{func.__name__} only takes numbers as the argument")
return inner_function
@exception_handler
def area_square(length):
print(length * length)
@exception_handler
def area_circle(radius):
print(3.14 * radius * radius)
@exception_handler
def area_rectangle(length, breadth):
print(length * breadth)
area_square(2)
area_circle(2)
area_rectangle(2, 4)
area_square("some_str")
area_circle("some_other_str")
area_rectangle("some_other_rectangle")
# Output:
# 4
# 12.568
# 8
# area_square only takes numbers as the argument
# area_circle only takes numbers as the argument
# area_rectangle only takes numbers as the argument
def area_square(length):
try:
print(length**2)
except TypeError:
print("area_square only takes numbers as the argument")
def area_circle(radius):
try:
print(3.142 * radius**2)
except TypeError:
print("area_circle only takes numbers as the argument")
def area_rectangle(length, breadth):
try:
print(length * breadth)
except TypeError:
print("area_rectangle only takes numbers as the argument")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment