Catching Exceptions Without a Variable

You do not always need to specify a variable in the Catch block. This can be the case in which you want to take the same action independently from the exception that occurred. For example, consider the following code:

Try    Dim result As String =        My.Computer.FileSystem.ReadAllText("C:\MyFile.txt")Catch ex As Exception    Console.WriteLine("A general error occurred")End Try

The ex variable is not being used and no specific exceptions are handled. So the preceding code can be rewritten as follows, without the ex variable:

Try    Dim result As String =        My.Computer.FileSystem.ReadAllText("C:\MyFile.txt")Catch    Console.WriteLine("A general ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.