rspec/rules/S2359/vbnet/rule.adoc

53 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Prefer the use of ``++Try ... Catch++`` blocks instead of ``++On Error++`` statements.
Visual Basic .NET and Visual Basic 2005 offer structured exception handling that provides a powerful, more readable alternative to the ``++On Error Goto++`` error handling from previous versions of Microsoft Visual Basic. Structured exception handling is more powerful because it allows you to nest error handlers inside other error handlers within the same procedure. Furthermore, structured exception handling uses a block syntax similar to the ``++If...Else...End If++`` statement. This makes Visual Basic .NET and Visual Basic 2005 code more readable and easier to maintain.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,vbnet]
2021-04-28 16:49:39 +02:00
----
Sub DivideByZero()
On Error GoTo nextstep
Dim result As Integer
Dim num As Integer
num = 100
result = num / 0
nextstep:
System.Console.WriteLine("Error")
End Sub
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,vbnet]
2021-04-28 16:49:39 +02:00
----
Sub DivideByZero()
Try
Dim result As Integer
Dim num As Integer
num = 100
result = num / 0
Catch
System.Console.WriteLine("Error")
End Try
End Sub
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace this use of "OnError" with structured error handling.
endif::env-github,rspecator-view[]