edit-icon download-icon

Handle errors

Last Updated: Mar 20, 2018

Alibaba Cloud Java SDK returns corresponding exceptions when an error occurs on the service side or the SDK side. These exceptions contain detailed error information, including the error code (ErrCode) and error message (ErrMsg).

  • ServerException is the exception returned by the corresponding Alibaba Cloud service.
  • ClientException is the exception returned by the Alibaba Cloud Java SDK.

You do not need to handle the exceptions returned by the Alibaba Cloud Java SDK. You do need to resolve the errors returned by the service.

For example, when the following error occurs, you have to modify the ID of the AccessKey according to the error message.

  1. com.aliyuncs.exceptions.ClientException: InvalidAccessKeyId.NotFound : Specified access key is not found.

If you want to handle the client errors returned by the Alibaba Cloud Java SDK, refer to the following example to write your codes:

  1. try {
  2. FooResponse response = client.getAcsResponse(request);
  3. // Handle the response
  4. // ...
  5. }catch (ServerException e){
  6. // You can add your own error handling logic here
  7. // For example, print the error message
  8. System.out.println("ErrorCode=" + e.getErrCode());
  9. System.out.println("ErrorMessage=" + e.getErrMsg());
  10. // If the problem is tricky, you can open a ticket and provide the RequestId to us
  11. System.out.println("ResponseId=" + e.getRequestId());
  12. }catch (ClientException e){
  13. // You can add your own error handling logic here
  14. // For example, print the error message
  15. System.out.println("ErrorCode=" + e.getErrCode());
  16. System.out.println("ErrorMessage=" + e.getErrMsg());
  17. }
Thank you! We've received your feedback.