All Products
Search
Document Center

Alibaba Cloud SDK:Handle an exception

Last Updated:May 23, 2023

This topic describes the exception types of the Classic SDK and how to handle an exception.

In the Classic SDK, exceptions are classified into two types: exceptions that occur on the server and exceptions that occur on the client. If a response contains ServerException, the exception occurs on the server. If a response contains ClientException, the exception occurs on the client. If an exception occurs and the server receives no requests, you cannot obtain the ID of the request that causes the exception. If the server receives the request that causes the exception, you can obtain the request ID. Then, you can use the request ID to ask Alibaba Cloud technical support for help.
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
try {
    AlibabaCloud::rpc()->product('ecs')->regionId('cn-hangzhou')->request();
} catch (ClientException $e) {
    // Obtain the error message.
    echo $e->getErrorMessage();
} catch (ServerException $exception) {
    // Obtain the error code.
    print_r($exception->getErrorCode());
    // Obtain the request ID.
    print_r($exception->getRequestId());
    // Obtain the error message.
    print_r($exception->getErrorMessage());
    // Obtain the result object.
    print_r($exception->getResult());
    // Obtain the response object.
    print_r($exception->getResult());
    // Obtain the request object.
    print_r($exception->getResult()->getRequest());
}