All Products
Search
Document Center

Alibaba Cloud SDK:Handle exceptions

Last Updated:Jun 14, 2024

Handle exceptions

You can use the try...except... statement to handle exceptions that occur on clients and servers.

#!/usr/bin/env python
# coding=utf-8
import os

from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526.DescribeImagesRequest import DescribeImagesRequest

client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
    'cn-hangzhou')

request = DescribeImagesRequest()
request.set_ImageOwnerAlias("system")
try:
    response = client.do_action_with_exception(request)
    print(response)
except ClientException as e:
    # Handle exceptions with caution in actual business scenarios and do not ignore exceptions in your project. In this example, error messages are only displayed. 
    # Exceptions that occur on clients.
    print(e)
except ServerException as e:
    # Handle exceptions with caution in actual business scenarios and do not ignore exceptions in your project. In this example, error messages are only displayed. 
    # Exceptions that occur on servers.
    print(e)

Solutions

Error code

Exception information

Solution

SDK.InvalidRegionID

can not find endpoint to access

The core library of the SDK does not run in the latest version. We recommend that you update the core library aliyun-python-sdk-core to the latest version or check whether the installed core library aliyun-python-sdk-core is valid.

SDK.EndpointResolvingError

No such region . Please check your region ID.

The region ID that you specify is invalid. You can query the supported regions to obtain the region IDs.

SDK.EndpointResolvingError

No endpoint for product .

No endpoints are available for the Alibaba Cloud service. We recommend that you perform the following operations to handle the exception. 1. Update the core library aliyun-python-sdk-core to the latest version. For example, you can update aliyun-python-sdk-ecs to the latest version.

2. Specify an endpoint to send an API request.