sys module for information
Lesson 9 of 16 in Coddy's Exception Handling in Python course.
exc_info function from sys module prints Exception details.
import sys
try:
#code which can cause exception
except:
print(sys.exc_info())Output:- ( class_name, standard_message, traceback_object )
It returns a tuple containing three values as above.
Challenge
EasyA function takes a key as argument. Complete function to fetch value for entered key. If key not found in dictionary, keyError will be raised. Handle it and print exception class name and standard message by using above way. Else, print value in else block.
Try it yourself
age={
'Peter':23,
'Lopez':24,
'Glenn':27,
'Jay':29
}
def fetch(key):
#your code goes hereAll lessons in Exception Handling in Python
4Customization/best practices
Customize Tracebacks2Exception Handling
try-exceptelseFinallyPrinting Exception messagePrinting Exception NameHandling Multiple Exceptionssys module for information