捕获和处理异常
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
遇到问题时,Health Connect 会针对 CRUD 操作抛出标准异常。您的应用应酌情捕获和处理每个异常。
HealthConnectClient
中的每种方法都会列出可能会被抛出的异常。一般而言,您的应用应处理以下异常:
异常 |
说明 |
建议的最佳实践 |
SecurityException
| 相应请求需要用到未被授予的权限时遇到问题。
| 为避免出现这种情况,请确保您已
声明使用“健康数据共享”数据类型
应用。此外,您还必须声明 Health Connect 权限
在清单文件中和在您的 activity 中。
|
IOException
| 从磁盘中读取和向其中写入数据时遇到问题。
| 为避免此类问题,请参考以下建议:
- 备份所有的用户输入。
- 能够处理在批量写入操作期间发生的任何问题。例如,确保进程越过问题并执行其余操作。
- 应用重试和退避策略来处理请求问题。
|
IllegalStateException
| 您可能遇到了以下情况之一:
- Health Connect 服务不可用。
- 相应请求的结构无效。例如,定期存储分区中的汇总请求,其中的
Instant 对象用于 timeRangeFilter 。
| 在发出请求之前,先处理可能存在的输入问题。最好能为变量赋值或在自定义函数中将它们用作参数,而不是直接在请求中使用这些值,以便顺利应用错误处理策略。
|
RemoteException
| SDK 所关联的底层服务内部或与该服务通信时出现错误。
例如,您的应用尝试删除具有给定 uid 的记录。不过,当应用在检查底层服务后发现记录不存在时,会抛出异常。
| 为避免此类问题,请参考以下建议:
- 在应用的数据存储区和 Health Connect 之间执行定期同步。
- 应用重试和退避策略来处理请求问题。
|
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Catch and handle exceptions\n\nHealth Connect throws standard exceptions for CRUD operations when an issue is\nencountered. Your app should catch and handle each of these exceptions as\nappropriate.\n\nEach method on `HealthConnectClient` lists the exceptions that can be thrown.\nIn general, your app should handle the following exceptions:\n\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| **Exception** | **Description** | **Recommended best practice** |\n|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `SecurityException` | There are issues encountered when the requests require permissions that aren't granted. | To avoid this, make sure that you've [declared use of Health Connect data types](/guide/health-and-fitness/health-connect/develop/frequently-asked-questions#declare-access) for your published app. Also, you must declare Health Connect permissions [in the manifest file](/guide/health-and-fitness/health-connect/develop/get-started#step-3) and [in your activity](/guide/health-and-fitness/health-connect/develop/get-started#step-4). \u003cbr /\u003e \u003cbr /\u003e |\n| `IOException` | There are issues encountered upon reading and writing data from disk. | To avoid this issue, here are some suggestions: \u003cbr /\u003e - Back up any user input. - Be able to handle any issues that occur during bulk write operations. For example, make sure the process moves past the issue and carry out the remaining operations. - Apply retries and backoff strategies to handle request issues. \u003cbr /\u003e |\n| `IllegalStateException` | One of the following scenarios has occurred: \u003cbr /\u003e - The Health Connect service isn't available. - The request isn't a valid construction. For example, an aggregate request in periodic buckets where an `Instant` object is used for the `timeRangeFilter`. \u003cbr /\u003e | Handle possible issues with the inputs first before doing a request. Preferably, assign values to variables or use them as parameters within a custom function instead of using them directly in your requests so that you can apply error handling strategies. |\n| `RemoteException` | Errors have occurred within, or in communicating with, the underlying service to which the SDK connects. For example, your app is trying to delete a record with a given `uid`. However, the exception is thrown after the app finds out upon checking in the underlying service that the record doesn't exist. | To avoid this issue, here are some suggestions: \u003cbr /\u003e - Perform regular syncs between your app's datastore and Health Connect. - Apply retries and backoff strategies to handle request issues. \u003cbr /\u003e |\n\n\u003cbr /\u003e"]]