This topic describes how Function Compute handle errors in the C# runtime environment.

Exceptions thrown by a function

If an error occurs when a function is being executed, Function Compute captures the error and returns an error message.

Sample statements:

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

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

        static void Main(string[] args){}
    }
}
The following response is returned when the function is invoked:
{
    "errorMessage": "oops",
    "errorType": "System.Exception",
    "stackTrace": [...]
}

When an error occurs, the HTTP header in the response to the function invocation contains X-Fc-Error-TypeUnhandledInvocationError. For more information about the types of errors in Function Compute, see Error types.

Active exit of a function

When you obtain error information by actively exiting a running function, you cannot obtain error information and stack information during the exit. This method is not recommended.

Sample statements:

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

namespace Example
{
    public class Hello
    {
        public async Task<Stream> StreamHandler(Stream input, IFcContext context)
        {
            System.Environment.Exit(1);
        }

        static void Main(string[] args){}
    }
}
The following response is returned when the function is invoked:
{
    "errorMessage": "Process exited unexpectedly before completing request (duration: 45ms, maxMemoryUsage: 49MB)"
}