Handle an exception

更新时间:
复制 MD 格式

Alibaba Cloud SDK V1.0 throws ServerException for server-side errors and ClientException for client-side errors. Catch these exceptions to retrieve error codes and request IDs for troubleshooting.

SDK V1.0 classifies exceptions into ServerException and ClientException. A ServerException indicates that the server received the request but failed to process it. You can retrieve the request ID from a ServerException and provide it to Alibaba Cloud technical support for troubleshooting. A ClientException indicates that the request did not reach the server, so no request ID is available.

public static void main(String[] args) {
        IClientProfile profile = DefaultProfile.getProfile(regionId, accesskeyId, accesskeySecret);
        profile.setHttpClientConfig(clientConfig);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        // Generate a request object in the SDK.
        DescribeRegionsRequest request = new DescribeRegionsRequest();
        try {
            DescribeRegionsResponse response = client.getAcsResponse(request);
        } catch (ServerException e) {
            // In this example, error information is displayed when an exception occurs. In actual business scenarios, handle exceptions with caution and never ignore exceptions in your project. 
            // Display the whole error output.
            e.printStackTrace();
            // Display the error code.
            System.out.println(e.getErrCode());
            // Display the request ID.
            System.out.println(e.getRequestId());
            // Display the error message.
            System.out.println(e.getErrMsg());
        } catch (ClientException e) {
            // In this example, error information is displayed when an exception occurs. In actual business scenarios, handle exceptions with caution and never ignore exceptions in your project. 
            // Display the whole error output.
            e.printStackTrace();
            // Display the error code.
            System.out.println(e.getMessage());
        }
}