Python Practice › sys Module Programs › Program 20
Current exception info
Write a Python program that reads the active exception with sys.exception().
Output
KeyError 'missing'
Approach
- Inside
except,sys.exception()returns the exception being handled (Python 3.11+).
Solution
import sys try: {}["missing"]except KeyError: e = sys.exception() print(type(e).__name__, e)Output
KeyError 'missing'
Learn the concept
New to this topic? Read the lesson, then come back.