The Classic SDK throws two types of exceptions: ServerException for server-side errors and ClientException for client-side errors.
In the Classic SDK, you catch ServerException for server-side errors and ClientException for client-side errors. If the server receives the request, the exception includes a RequestId that you can provide to Alibaba Cloud technical support for troubleshooting. If the request does not reach the server, no RequestId is available.
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 to send requests.
IClientProfile profile = DefaultProfile.GetProfile(
// The region ID.
"<your-region-id>",
// Get the AccessKey ID of the Resource Access Management (RAM) user from an environment variable.
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// Get the AccessKey secret of the RAM user from an environment variable.
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
try
{
// Create a request.
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.PageSize = 10;
// Send the request and get the response.
DescribeInstancesResponse response = client.GetAcsResponse(request);
System.Console.WriteLine(response.TotalCount);
}
catch (ServerException e)
{
System.Console.WriteLine(e.ToString());
// Add your own error handling logic here.
// For example, print the specific error message.
Console.WriteLine("ErrorCode=" + e.ErrorCode);
Console.WriteLine("ErrorMessage=" + e.ErrorMessage);
// If you encounter a difficult problem, submit a ticket and provide the RequestId.
Console.WriteLine("ErrorCode=" + e.RequestId);
}
catch (ClientException ex)
{
System.Console.WriteLine(ex.ToString());
// Add your own error handling logic here.
// For example, print the specific error message.
Console.WriteLine("ErrorCode=" + ex.ErrorCode);
Console.WriteLine("ErrorMessage=" + ex.ErrorMessage);
}
}
}
}