Printing Exception Name
Lesson 7 of 16 in Coddy's Exception Handling in Python course.
We can print Exception name using class attribute.
try:
#code which can cause exception
except [ExceptionName] as [object_name]:
print(object_name.__class__)Challenge
EasyYou will have list as input. Write a program to sum all elements in a list. If it encounters an exception during summation, it will raise an Exception. Otherwise, it will print summation. The Exception type is TypeError and text as "Exception name:Standard message"
Try it yourself
def summation(my_list):
#your code hereAll lessons in Exception Handling in Python
4Customization/best practices
Customize Tracebacks2Exception Handling
try-exceptelseFinallyPrinting Exception messagePrinting Exception NameHandling Multiple Exceptionssys module for information