use_rethrow_when_possible
使用 rethrow 重新拋出捕獲的例外。
詳細資訊
#務必 使用 rethrow 重新拋出捕獲的例外。
由於 Dart 提供 rethrow 作為一項功能,應加以使用以提升簡潔度和可讀性。
不良示範
dart
try {
somethingRisky();
} catch(e) {
if (!canHandle(e)) throw e;
handle(e);
}
良好示範
dart
try {
somethingRisky();
} catch(e) {
if (!canHandle(e)) rethrow;
handle(e);
}
啟用
#若要啟用 use_rethrow_when_possible
規則,請在您的 analysis_options.yaml
檔案中的 linter > rules 下方新增 use_rethrow_when_possible
analysis_options.yaml
yaml
linter:
rules:
- use_rethrow_when_possible
如果您改為使用 YAML 對應語法來設定 linter 規則,請在 linter > rules 下方新增 use_rethrow_when_possible: true
analysis_options.yaml
yaml
linter:
rules:
use_rethrow_when_possible: true
除非另有說明,否則本網站上的文件反映 Dart 3.7.1 版本。頁面最後更新於 2025-03-07。 檢視原始碼 或回報問題。