錯誤處理
更新時間 2024-12-10 11:25:41
最近更新時間: 2024-12-10 11:25:41
分享文章
如果函數在執行過程中拋出異常,那么會被函數計算捕獲并返回異常信息。
如下示例代碼中,要求輸入值必須大于0,否則會拋出異常:
#?-*-?coding:?utf-8?-*-
def?handler(event,?context):
????if?int(event)?<=?0:
????????raise?ValueError("Input?Must?be?greater?than?0")???
????return?'Your?input?is:?'?+?str(event)
當我們輸入0時,函數被執行后函數計算會返回如下異常信息:
{
????"errorMessage":?"Input?Must?be?greater?than?0",
????"errorType":?"ValueError",
????"stackTrace":?[
????????[
????????????"File?\"/code/index.py\"",
????????????"line?5",
????????????"in?handler",
????????????"raise?ValueError(\"Input?Must?be?greater?than?0\")"
????????]
????]
}
異常信息包含如下三個字段:
| 字段 | 類型 | 解釋說明 |
|---|---|---|
| errorMessage | String | 異常信息。 |
| errorType | String | 異常類型。 |
| stackTrace | List | 異常堆棧。 |