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 error occurred")
End Try
Whichever exception occurs, the code shows the specified message. This also works with regard to the rethrow technique. The following code simply rethrows the proper exception to the caller:
Try
Dim result As String =
My.Computer.FileSystem.ReadAllText("C:MyFile.txt")
Catch
Throw
End Try
18.118.208.97