Exception handling
Learn about exception types in the V2.0 SDK and how to handle them.
Exception types
The V2.0 SDK classifies exceptions into these types:
-
TeaUnretryableException: Thrown when network issues persist after all retries are exhausted. Call
exception.getLastRequestto inspect the failing request. -
TeaException: Indicates a business error. Use these parameters to troubleshoot:
-
code: The OpenAPI business error code.
-
message: The error message, which includes the RequestId.
-
data: Detailed error information returned by the server.
-
This example only prints output for demonstration. In production, handle exceptions properly—propagate them, log them, or attempt recovery to ensure system stability.
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();
// Get the AccessKey ID of the RAM user from an environment variable.
config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
// Get the AccessKey secret of the RAM user from an environment variable.
config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// 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) {
// This is for demonstration only. Handle exceptions with caution and do not ignore them in your project.
ue.printStackTrace();
// Print the error message.
System.out.println(ue.getMessage());
// Print the request record.
System.out.println(ue.getLastRequest());
} catch (TeaException e) {
// This is for demonstration only. Handle exceptions with caution and do not ignore them in your project.
e.printStackTrace();
// Print the error code.
System.out.println(e.getCode());
// Print the error message, which includes the RequestId.
System.out.println(e.getMessage());
// Print the specific content returned by the server.
System.out.println(e.getData());
}
}
}
Handle exceptions
To troubleshoot API call exceptions:
-
Copy the RequestId from the exception and use OpenAPI Diagnostics to identify the cause. OpenAPI Diagnostics.
-
Look up the error code in the relevant product documentation. For example, Short Message Service errors are listed in API error codes.
-
Check the FAQ for a solution.
-
Contact technical support.