![](/img/trans.png)
[英]ScheduledThreadPoolExecutor stops executing with caught exceptions
[英]Dart / Flutter - Debugger stops on caught exceptions
在檢查 API 端點(確定連接狀態)的相對簡單的代碼塊中,我依靠try..catch
作為驗證應用程序是否可以與服務器通信的機制。
我遇到的問題是,在調試時,調試器總是在連接線上停止(當應用程序離線時),即使我在內部處理錯誤。
Future<bool> isOnline() async {
try {
// VSCode debugger always stops on this line when no connection
await http
.get('${consts.apiBaseUrl}/api/ping')
.timeout(Duration(seconds: normalTimeoutLength))
.catchError(
(_) {
// Trying catchError on the Future
_isOnline = false;
return false;
},
);
_isOnline = true;
return true;
} on HttpException catch (_) {
// Trying to catch HTTP Exceptions
_isOnline = false;
return false;
} on SocketException catch (_) {
// Trying to catch Socket Exceptions
_isOnline = false;
return false;
}
}
這是 Dart VM 的一個限制。 它不能正確檢測用catchError()
捕獲的異常,因此它會導致調試器暫停它們。 這里有一些關於這個的討論:
https://github.com/flutter/flutter/issues/33427#issuecomment-504529413
如果您單擊繼續/恢復,行為應該沒有區別,但作為一種解決方法,您可以將代碼轉換為使用真正的try
/ catch
而不是catchError()
或取消選中調試側欄中的選項以中斷未捕獲的異常(盡管顯然這也會影響真正的未捕獲異常——盡管在 Flutter 中它們不太常見,因為框架捕獲了大多數異常)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.