try:
a = int(input('輸入 0~9:'))
if a>10:
raise
print(a)
except :
print('有錯誤喔~')
else:
print('沒有錯!繼續執行!') # 完全沒錯才會執行這行
finally:
print('管他有沒有錯,繼續啦!') # 不論有沒有錯都會執行這行
raise ValueError("Only one of TIME or LOTs can be input!")
import sys
import traceback
try:
test_func()
except Exception as e:
cl, exc, tb = sys.exc_info() # 取得Call Stack
last_call_stack = traceback.extract_tb(tb)[-1] # 取得Call Stack的最後一筆資料
print('錯誤發生的檔案名稱', last_call_stack[0])
# 錯誤發生的檔案名稱 /Users/dinoinchen/Utilites/Python-stock/test.py
print('錯誤發生的行號', last_call_stack[1])
# 錯誤發生的行號 6
print('錯誤發生的函數名稱', last_call_stack[2])
# 錯誤發生的函數名稱 test_func
print('錯誤本身', last_call_stack[3])
# 錯誤本身 raise RuntimeError("Test ERR!")
import traceback
try:
...
except Exception as e:
print(traceback.format_exc()) # 獲取當前的堆疊蹤跡,顯示例外發生的檔案名和行數
https://www.runoob.com/python/python-exceptions.html
from fastapi import HTTPException
raise HTTPException(status_code=404, detail="TEST ERROR! 測個錯誤!")
def catch_exception_to_http_response(e):
error_class = e.__class__.__name__ # 取得錯誤類型
detail = e.args[0] if len(e.args[0])>0 else "Oops something is wrong!" # 取得詳細內容
cl, exc, tb = sys.exc_info() # 取得Call Stack
last_call_stack = traceback.extract_tb(tb)[-1] # 取得Call Stack的最後一筆資料
file_fame = last_call_stack[0] # 取得發生的檔案名稱 >D:\Dinoin\workspace\yield-prediction-cip\api\Helper.py
line_num = last_call_stack[1] # 取得發生的行號 >99
func_name = last_call_stack[2] # 取得發生的函數名稱 >funcName
print("File \"{}\", line {}, in {}: [{}] {}".format(file_fame, line_num, func_name, error_class, detail))
print('錯誤本身:', last_call_stack[3]) # type=str
raise HTTPException(status_code=400, detail=detail)
from utility.functions import catch_exception_to_http_response as catch
def main():
try:
return ...
except Exception as e:
raise catch(e)
https://fastapi.tiangolo.com/tutorial/handling-errors/ https://steam.oxxostudio.tw/category/python/basic/try-except.html