rspec/rules/S2359/vbnet/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

53 lines
1.3 KiB
Plaintext

== Why is this an issue?
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
[source,vbnet]
----
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
[source,vbnet]
----
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[]