錯誤處理
更新時間 2024-12-10 11:37:50
最近更新時間: 2024-12-10 11:37:50
分享文章
本文介紹C#運行環境的錯誤處理。
如果函數在執行過程中拋出異常,那么會被函數計算捕獲并返回異常信息。
如下示例代碼中,簡單拋出了一個異常:
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Serverless.Cf;
namespace Example
{
public class Hello
{
public async Task<Stream> Handler(Stream input, ICfContext context)
{
throw new Exception("something wrong");
}
static void Main(string[] args) { }
}
}
函數被執行后函數計算會返回如下異常信息:
{
"errorMessage": "something wrong",
"errorType": "Exception",
"stackTrace": [
" at Example.Hello.Handler(Stream input, ICfContext context) in XXX\\HelloApp\\Program.cs:line 15",
...
]
}
異常信息字段說明:
| 字段 | 類型 | 解釋說明 |
|---|---|---|
| errorMessage | string | 異常信息。 |
| errorType | string | 異常類型。 |
| stackTrace | string[] | 異常堆棧。 |