rspec/rules/S2737/vbnet/rule.adoc
Fred Tingaud ad4a486e52
Modify Rule S2737: LaYC catch rethrow
Co-authored-by: Amélie Renard <44666826+amelie-renard-sonarsource@users.noreply.github.com>
Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
2023-06-22 14:04:34 +02:00

68 lines
1.3 KiB
Plaintext

== Why is this an issue?
A `Catch` clause that only rethrows the caught exception has the same effect as omitting the `Catch` altogether and letting it bubble up automatically.
[source,vbnet]
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As Exception
Throw
End Try
----
Such clauses should either be removed or populated with the appropriate logic.
[source,vbnet]
----
Dim s As String = File.ReadAllText(fileName)
----
or
[source,vbnet]
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As Exception
logger.LogError(e)
Throw
End Try
----
=== Exceptions
This rule will not generate issues for `Catch` blocks if they are followed by a `Catch` block for a more general exception type that does more than just rethrowing the exception.
[source,vbnet]
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As IOException 'Compliant by exception: removing it would change the logic
Throw
Catch e As Exception 'Compliant: does more than just rethrow
logger.LogError(e)
Throw
End Try
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]