All Products
Search
Document Center

Alibaba Cloud SDK:Exception handling

Last Updated:Oct 28, 2024

Exceptions that may occur when you use Alibaba Cloud SDK V2.0 for .NET are classified into the following types:

  • TeaUnretryableException: This type of exception is caused by network issues. TeaUnretryableException is thrown if the number of retries reaches the upper limit. You can call the exception.getLastRequest method to obtain the information about the API request for which the exception is thrown.

  • TeaException: This type of exception is caused by business errors. The following three parameters are provided to handle this type of exception:

    • code: the error code that is returned when the exception occurs.

    • message: the error message that is returned when the exception occurs. The message contains the ID of the API request for which the exception is thrown.

    • data: the detailed error information that is returned by the server for the exception.

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 
    {

        /**
         * Use your AccessKey ID and AccessKey secret to initialize a client.
         * @param accessKeyId
         * @param accessKeySecret
         * @return Client
         * @throws Exception
         */
        public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient()
        {
            AlibabaCloud.OpenApiClient.odels.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment. 
                AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. 
                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)
            {
                // The error message.
                Console.WriteLine(error.Message);
                // The URL of the corresponding error diagnostics page.
                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 }
                });
                // The error message.
                Console.WriteLine(error.Message);
                // The URL of the corresponding error diagnostics page.
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
        }


    }
}