rspec/rules/S2737/vbnet/rule.adoc

68 lines
1.3 KiB
Plaintext
Raw Normal View History

== 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.
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,vbnet]
2020-06-30 12:48:07 +02:00
----
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
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,vbnet]
2020-06-30 12:48:07 +02:00
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As Exception
logger.LogError(e)
Throw
End Try
----
=== Exceptions
2020-06-30 12:48:07 +02:00
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.
2020-06-30 12:48:07 +02:00
2021-02-02 15:02:10 +01:00
[source,vbnet]
2020-06-30 12:48:07 +02:00
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As IOException 'Compliant by exception: removing it would change the logic
2020-06-30 12:48:07 +02:00
Throw
Catch e As Exception 'Compliant: does more than just rethrow
2020-06-30 12:48:07 +02:00
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[]