rspec/rules/S1679/dart/rule.adoc
Marharyta c3aa4b07c5
Add more Dart rules (#4062)
* Modify rule S6582: Add Dart language

* Modify rule S6606: Add Dart language

* Modify rule S6207: Add Dart language

* Modify rule S1116: Add Dart example with empty "else"

* Modify rule S927: Add Dart language

* Modify rule S1155: Add Dart language

* Modify rule S2933: Add Dart language

* Modify rule S2971: Add Dart language

* Modify rule S4123: Add Dart language

* Modify rule S120: Add Dart language

* Modify rule S1679: Add Dart language

* Modify rule S2159: Add Dart language

* Modify rule S3257: Add Dart language

* Modify rule S6619: Add Dart language

* Modify rule S3562: Add Dart language

* Modify rule S3240: Add Dart language

* Modify rule S5416: Add Dart language

* Modify rule S2175: Add Dart language

* Modify rule S3962: Add Dart language

* Modify rule S2471: Add Dart language

* Modify rule S3512: Add Dart language

* Modify rule S2432: Add Dart language
2024-07-18 13:20:47 +02:00

52 lines
1.2 KiB
Plaintext

== Why is this an issue?
In dart `throw` is used no initiate an exception. Usually this is enough to just catch it and handle. However, there are cases when the exceptions needs to be propagated further after being caught. This allows a handling of the exception on different levels.
In such case it's recommended to use `rethrow` instead of just `throw`, to preserve the original stacktrace.
[source,dart]
----
try {
...
} catch (ex, stacktrace) {
rethrow ex; // preserves the original exception with its stacktrace
}
----
== How to fix it
The rule raises an issue when the argument of the `throw` expression is an unmodified copy of the caught exception.
=== Code examples
==== Noncompliant code example
[source,dart,diff-id=1,diff-type=noncompliant]
----
void foo() {
try {
methodThrowsException();
} catch (ex) {
// ...
throw ex; // Noncompliant
}
}
----
==== Compliant solution
[source,dart,diff-id=1,diff-type=compliant]
----
void foo() {
try {
methodThrowsException();
} catch (ex) {
// ...
rethrow ex;
}
}
----
== Resources
* https://dart.dev/tools/linter-rules/use_rethrow_when_possible[Dart Lint rule]