Current exception info

Easysys Module Programs · 20 of 20

Write a Python program that reads the active exception with sys.exception().

Advertisement
Output
KeyError 'missing'

Approach

  1. Inside except, sys.exception() returns the exception being handled (Python 3.11+).

Solution

Python
import sys try:    {}["missing"]except KeyError:    e = sys.exception()    print(type(e).__name__, e)
Output
KeyError 'missing'
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More sys module programs

← All 20 sys Module Programs