Python sys.exc_info() method



The Python sys.exc_info() method that returns a tuple containing information about the most recent exception caught by an except clause. The tuple consists of three elements, they are the exception type, the exception value and a traceback object.

This method is particularly useful for error handling and debugging as it provides detailed information about the exception context. By using 'sys.exc_info()' method developers can programmatically access and manipulate exception details by improving error logging and handling capabilities within their applications.

Syntax

Following is the syntax and parameters of Python sys.exc_info() method −

sys.exc_info()

Parameter

This method does not return any values.

Return value

This method returns tuple containing three values (type, value, traceback).

Example 1

Following is the basic example in which, a ZeroDivisionError is raised and caught. The sys.exc_info() method retrieves information about the exception −

import sys

try:
    1 / 0
except ZeroDivisionError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print(f"Exception type: {exc_type}")
    print(f"Exception value: {exc_value}")
    print(f"Traceback object: {exc_traceback}")

Output

Exception type: <class 'ZeroDivisionError'>
Exception value: division by zero
Traceback object: <traceback object at 0x0000016DCBD35000>

Example 2

This example uses sys.exc_info() method to retrieve the exception details and then uses the traceback module to print the traceback details −

import sys
import traceback

def divide(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print(f"Exception type: {exc_type}")
        print(f"Exception value: {exc_value}")
        print("Traceback details:")
        traceback.print_tb(exc_traceback)

divide(1, 0)

Output

Exception type: <class 'ZeroDivisionError'>
Exception value: division by zero
Traceback details:
  File "\sample.py", line 6, in divide
    return a / b
           ~~^~~

Example 3

Now in this example we show how nested exception handling can be structured in custom functions −

import sys
import traceback

try:
    try:
        result = int('not a number')
    except ValueError:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("Inner exception info:")
        print(f"  Type: {exc_type}")
        print(f"  Value: {exc_value}")
        traceback.print_tb(exc_traceback)
        # Re-raise the exception to the outer try block
        raise
except Exception as e:
    print("Outer exception info:")
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print(f"  Type: {exc_type}")
    print(f"  Value: {exc_value}")
    traceback.print_tb(exc_traceback)

Output

Inner exception info:
  Type: <class 'ValueError'>
  Value: invalid literal for int() with base 10: 'not a number'
  File "\sample.py", line 6, in <module>
    result = int('not a number')
             ^^^^^^^^^^^^^^^^^^^
Outer exception info:
  Type: <class 'ValueError'>
  Value: invalid literal for int() with base 10: 'not a number'
  File "\sample.py", line 6, in <module>
    result = int('not a number')
             ^^^^^^^^^^^^^^^^^^^
python_modules.htm
Advertisements