Handle exceptions

更新时间:
复制 MD 格式

Alibaba Cloud SDK for Python throws ClientException and ServerException errors that you can catch with try...except... blocks.

Handle exceptions

Use the try...except... statement to handle client-side and server-side exceptions.

#!/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 message

Solution

SDK.InvalidRegionID

can not find endpoint to access

The SDK core library is outdated. Update aliyun-python-sdk-core to the latest version, or verify that the installed aliyun-python-sdk-core package is valid.

SDK.EndpointResolvingError

No such region . Please check your region ID.

The specified region ID is invalid. Query the list of supported regions to obtain valid region IDs.

SDK.EndpointResolvingError

No endpoint for product .

No endpoint is available for the specified service. To resolve this issue: 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. Manually specify an endpoint for the API request.