Nếu Python code throw exception, chúng ta có thể catch nó và print type, error message, traceback và lấy thông tin như file name và line number trong Python script nơi exception xảy ra.
Chúng ta có thể tìm type, value, traceback parameters của error.
Type cho type của exception xảy ra; value chứa error message; traceback chứa stack snapshot và nhiều thông tin chi tiết khác về error message.
Hàm sys.exc_info() trả về tuple của ba attributes này, và raise statement có form ba arguments chấp nhận ba phần này.
Lấy exception type, file number và line number trong sample code:
import sys, os
try:
raise NotImplementedError("No error")
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)