All Products
Search
Document Center

Function Compute:Error handling

Last Updated:Apr 01, 2026

When a C# function in Function Compute throws an exception or exits unexpectedly, the platform captures what it can and returns structured error details. Understanding this behavior helps you write handlers that fail gracefully and remain debuggable.

Errors thrown by a function

When an error occurs during function execution, Function Compute captures the exception and returns error details in the response body.

The following example throws an unhandled exception:

using System;
using System.IO;
using System.Threading.Tasks;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;

namespace Example
{
    public class Hello
    {
        public async void StreamHandler(Stream input, IFcContext context)
        {
            throw new Exception("oops");
        }

        static void Main(string[] args){}
    }
}

When you invoke this function, the response body contains:

{
    "errorMessage": "oops",
    "errorType": "System.Exception",
    "stackTrace": [...]
}

The response also includes the HTTP header X-Fc-Error-Type: UnhandledInvocationError. For a complete list of Function Compute error types, see Basics.

Proactive exit of a function

Calling System.Environment.Exit(1) inside a function handler terminates the process before Function Compute can capture error details. The response contains only a generic message — no error type and no stack trace:

{
    "errorMessage": "Process exited unexpectedly before completing request (duration: 45ms, maxMemoryUsage: 49MB)"
}

Avoid System.Environment.Exit() in function handlers. If you need to handle shutdown signals or clean up resources before termination, use structured exception handling instead.