All Products
Search
Document Center

Alibaba Cloud SDK:Exception handling

Last Updated:Aug 06, 2024

This topic describes the exception types of Alibaba Cloud SDK V2.0 for Java and how to handle exceptions.

Exceptions that may occur when you use Alibaba Cloud SDK V2.0 for Java are classified into the following types:

  • TeaUnretryableException: This type of exception is caused by network issues. TeaUnretryableException is thrown if the number of retries reaches the upper limit. You can call the exception.getLastRequest method to obtain the information about the API request for which the exception is thrown.

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

Important

In this example, error messages are displayed for reference only. In the actual business scenario, do not ignore exceptions in your project. We recommend that you properly handle exceptions by performing operations such as reporting exceptions, recording logs, and performing retries. This helps ensure the robustness and stability of your system.

import com.aliyun.ecs20140526.models.DescribeRegionsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.tea.ValidateException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class Sample {
    public static void main(String[] args) throws Exception {
        Config config = new com.aliyun.teaopenapi.models.Config();
        // Obtain the AccessKey ID of the Resource Access Management (RAM) user from environment variables.
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        // Obtain the AccessKey secret of the RAM user from environment variables.
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        // Specify the region ID.
        config.setRegionId("<regionId>");
        com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);

        DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
        try {
            client.describeRegions(describeRegionsRequest);
        } catch(TeaUnretryableException ue) {
            // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. 
            ue.printStackTrace();
            // Obtain the error message.
            System.out.println(ue.getMessage());
            // Obtain the request.
            System.out.println(ue.getLastRequest());
        } catch (TeaException e) {
            // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. 
            e.printStackTrace();
            // Obtain the error code.
            System.out.println(e.getCode());
            // Obtain the error message. The error message contains the request ID.
            System.out.println(e.getMessage());
            // Obtain the detailed error information that is returned by the server.
            System.out.println(e.getData());
        }
    }
}