Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 249ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
e

exception-handling

Recently created Least recently created Recently updated Least recently updated

timmy / 使用 traceback 來捕捉與記錄異常

Last active 5 hours ago debugging exception-handling logging python traceback

traceback 模組可用於捕捉、格式化和記錄 Python 例外發生時的詳細資訊,有助於錯誤診斷與日誌記錄。

0 0 0 1
1 import traceback
2
3 def faulty_function():
4 return 1 / 0 # 這將觸發 ZeroDivisionError
5
6 try:
7 faulty_function()
8 except Exception as e:
9 error_message = f"Exception occurred: {str(e)}"
10 stack_trace = traceback.format_exc()