Handle TeaException and TeaUnretryableException thrown by Alibaba Cloud SDK V2.0 for .NET during API calls.
Alibaba Cloud SDK V2.0 for .NET throws two types of exceptions during API calls:
TeaUnretryableException: Thrown when the retry limit is reached due to network-level failures such as connectivity issues, DNS resolution failures, or request timeouts. Call
exception.getLastRequestto retrieve details about the request that exhausted all retries.-
TeaException: Thrown when the service returns an error response due to invalid parameters, missing permissions, resource conflicts, or quota limits. This exception provides three properties:
code: the error code returned by the service.message: the error message, including the request ID for the failed API call.data: additional error details from the service.data["Recommend"]contains a diagnostics URL.
Example
The following example calls DescribeInstances and handles both exception types. Before you run the example, set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to your AccessKey ID and AccessKey secret.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client with an AccessKey ID and AccessKey secret.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// Required. Set the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable.
AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Required. Set the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable.
AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
};
config.Endpoint = "ecs.cn-qingdao.aliyuncs.com";
return new AlibabaCloud.SDK.Ecs20140526.Client(config);
}
public static void Main(string[] args)
{
AlibabaCloud.SDK.Ecs20140526.Client client = CreateClient();
AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesRequest describeInstancesRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesRequest
{
RegionId = "cn-qingdao",
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
client.DescribeInstancesWithOptions(describeInstancesRequest, runtime);
}
catch (TeaException error)
{
// Error message
Console.WriteLine(error.Message);
// Diagnostics URL
Console.WriteLine(error.Data["Recommend"]);
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Error message
Console.WriteLine(error.Message);
// Diagnostics URL
Console.WriteLine(error.Data["Recommend"]);
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
}
}
}