Exceptions in Visual Basic
Try, a filtered Catch, Finally and a custom error of your own.
Try
Throw New RecipeError("no yeast on hand")
Catch ex As RecipeError When ex.Message.Contains("yeast")
Console.WriteLine($"specific: {ex.Message}")
Catch ex As Exception
Console.WriteLine("anything else lands here")
Finally
Console.WriteLine("kitchen cleaned")
End Try
Class RecipeError
Inherits Exception
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
How it works
- Inheriting
Exceptionnames a failure precisely. Catch ... Whenonly catches if the filter expression holds.- The general
Catch ex As Exceptiontakes whatever remains. Finallyruns on success and failure alike.
Keywords and builtins used here
CatchClassEndFinallyInheritsMyBaseNewPublicRecipeErrorStringSubThrowTryWhen
The run, in numbers
- Lines
- 17
- Characters to type
- 380
- Tokens
- 78
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 52 seconds.
Step 2 of 3 in Files & errors, step 26 of 29 in Language basics.