擷取及處理例外狀況
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Health Connect 會在問題發生時擲回 CRUD 作業的標準例外狀況
。您的應用程式應擷取並處理這些例外狀況,
或適當。
HealthConnectClient
的每個方法都列出了可以擲回的例外狀況。
一般來說,應用程式應處理下列例外狀況:
例外狀況 |
說明 |
建議最佳做法 |
SecurityException
| 請求驗證時發生問題
未授予的權限
| 如要避免這種情況,請務必
您發布的已發布應用程式宣告使用 Health Connect 資料類型
應用程式。此外,您也必須宣告 Health Connect 權限
資訊清單檔案和活動中。
|
IOException
| 讀取及寫入以下來源的資料時發生問題
磁碟。
| 為避免這個問題,請參考以下幾點建議:
- 將所有使用者輸入內容備份。
- 確認自己有辦法處理大量寫入作業期間發生的任何問題。適用對象
例如,請確認程序會避開問題,
其他作業。
- 套用重試和輪詢策略來處理要求問題。
|
IllegalStateException
| 發生下列任一情況:
- 無法使用 Health Connect 服務。
- 要求為無效結構。例如
定期值區,其中
Instant 物件用於 timeRangeFilter 。
| 先處理輸入內容的潛在問題,然後再提出要求。
建議您為變數指派值,或將其用做自訂內的參數
函式,而不要直接在要求中使用,以便
錯誤處理策略
|
RemoteException
| 在通訊中發生錯誤,或在通訊中發生錯誤
以及 SDK 連線的基礎服務
舉例來說:
應用程式嘗試刪除具有指定 uid 的記錄。不過
會在應用程式發現後,
記錄不存在。
| 為避免這個問題,請參考以下幾點建議:
- 在應用程式的資料儲存庫和 Health Connect 間定期執行同步作業。
- 套用重試和輪詢策略來處理要求問題。
|
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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"]]