Python Print Error Detail

import sys def print_error(error_message): # Get the traceback object tb = sys.exc_info()[2] if tb is not None: f = tb.tb_frame.f_code print(f"\n>>> ERROR DETAILS:") print(f"- File: {f.co_filename}") print(f"- Function: {f.co_name}") print(f"- Line: {tb.tb_lineno}") print(f"- Variables: {tb.tb_frame.f_locals}") print(f"- Error Message: {error_message}\n") else: print(f"Error Message: {error_message} (No traceback found)") # USAGE EXAMPLE: try: # Let's create a sample error result = 10 / 0 except Exception as e: print_error(e)
    

Google