"on Exception" Doesn't Handle TypeError in External Data Validation #60024
Labels
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
closed-as-intended
Closed as the reported issue is expected behavior
Dart SDK Version: 3.6.1
Flutter Version: 3.27.3
Problem description:
I am trying to follow the
avoid_catches_without_on_clauses
linter rule by usingon Exception
for error handling. However, I’ve encountered an issue whereTypeError
(a subclass of Error) isn’t caught when processing invalid external data (e.g., API responses). The linter flag for using catch (e) forces me to useon Exception
, but this doesn’t catchTypeError
, leading to test failures when dealing with invalid data types in API responses.Expected Behavior:
on Exception
should catch errors likeTypeError
, which occur during external data validation.Actual Behavior:
on Exception
does not catchTypeError
, causing the test to fail when an invalid type is encountered.Steps to reproduce: First Option
Test Code: Line 152
Implementation Code: Line 29
Steps to Reproduce: Second option
Behavior
With on Exception:
❌ Test fails (TypeError propagates and isn't caught by on Exception).
With catch (e) (violates the linter rule):
The text was updated successfully, but these errors were encountered: