All Products
Search
Document Center

Alibaba Cloud SDK:Handle an exception

Last Updated:Sep 15, 2023

This topic describes the exception types of the Classic SDK and how to handle an exception.

In the Classic SDK, exceptions are classified into two types: exceptions that occur on the server and exceptions that occur on the client. If a response contains ServerException, the exception occurs on the server. If a response contains ClientException, the exception occurs on the client. If an exception occurs and the server receives no requests, you cannot obtain the ID of the request that causes the exception. If the server receives the request that causes the exception, you can obtain the request ID. Then, you can use the request ID to ask Alibaba Cloud technical support for help.

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Ecs.Model.V20140526;
namespace AlibabaCloud.SDK.Sample

{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a client instance to initiate a request.
            IClientProfile profile = DefaultProfile.GetProfile(
                // The region ID.
                "<your-region-id>",
                // Obtain the AccessKey ID of a Resource Access Management (RAM) user from environment variables.
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Obtain the AccessKey secret of the RAM user from environment variables.
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            DefaultAcsClient client = new DefaultAcsClient(profile);

            try
            {
                // Construct a request.
                DescribeInstancesRequest request = new DescribeInstancesRequest();
                request.PageSize = 10;

                // Initiate the request and obtain the response.
                DescribeInstancesResponse response = client.GetAcsResponse(request);
                System.Console.WriteLine(response.TotalCount);
            }
            catch (ServerException e)
            {
                System.Console.WriteLine(e.ToString());
                // You can specify the error handling logic.
                // For example, display the error information.
                Console.WriteLine("ErrorCode=" + e.ErrorCode);
                Console.WriteLine("ErrorMessage=" + e.ErrorMessage);
                // If the error persists, you can submit a ticket and provide the request ID to ask for technical support.
                Console.WriteLine("ErrorCode=" + e.RequestId);
            }
            catch (ClientException ex)
            {
                System.Console.WriteLine(ex.ToString());
                // You can specify the error handling logic.
                // For example, display the error information.
                Console.WriteLine("ErrorCode=" + ex.ErrorCode);
                Console.WriteLine("ErrorMessage=" + ex.ErrorMessage);
            }
        }
    }
}