Menu
Coddy logo textTech

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 icon

Challenge

Easy

A 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 here

All lessons in Exception Handling in Python