All Products
Search
Document Center

Alibaba Cloud SDK:Exception handling

Last Updated:May 16, 2024

This topic describes the exception types of Alibaba Cloud Darabonba SDK for Python and how to handle these exceptions.

Exception types and sample code

Exceptions that may occur when you use Alibaba Cloud Darabonba SDK for Python are classified into the following types:

  1. UnretryableException: This type of exception is caused by network issues. TeaUnretryableException is thrown if the number of retries reaches the upper limit.

  2. 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.

  3. import os
    
    from Tea.exceptions import UnretryableException, TeaException
    from alibabacloud_ecs20140526.models import DescribeImagesRequest
    from alibabacloud_ecs20140526.client import Client
    from alibabacloud_tea_openapi.models import Config
    
    '''Example of sending a request to call the Elastic Compute Service (ECS) API'''
    # Initialize an SDK client by using Config.
    config = Config(
        access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
        access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
        region_id='<regionId>'
    )
    client = Client(config)
    # Initialize a request.
    request = DescribeImagesRequest(image_id='<image-id>', region_id='<regionId>')
    try:
        response = client.describe_images(request)
    except UnretryableException as e:
        # Network exceptions.
        print(e)
    except TeaException as e:
        # Business exceptions.
        print(e)
    except Exception as e:
        # Other exceptions.
        print(e)