rspec/rules/S2737/vbnet/rule.adoc

48 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-12-23 14:59:06 +01:00
A ``Catch`` clause that only rethrows the caught exception has the same effect as omitting the ``Catch`` altogether and letting it bubble up automatically, but with more code and the additional detriment of leaving maintainers scratching their heads.
2020-06-30 12:48:07 +02:00
Such clauses should either be eliminated or populated with the appropriate logic.
== Noncompliant Code Example
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As Exception
Throw
End Try
----
== Compliant Solution
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As Exception
logger.LogError(e)
Throw
End Try
----
or
2020-06-30 12:48:07 +02:00
----
Dim s As String = File.ReadAllText(fileName)
----
== Exceptions
2020-12-23 14:59:06 +01:00
This rule will not generate issues for ``Catch`` blocks with just ``Throw`` inside 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
----
Dim s As String = ""
Try
s = File.ReadAllText(fileName)
Catch e As IOException 'Compliant, if removed will change the logic
Throw
Catch e As Exception 'Compliant, does more than just rethrow
logger.LogError(e)
Throw
End Try
----