![]() ![]() | ||
You also can use multiple Catch statements when you filter exceptions. Here's an example that specifically handles overflow, invalid argument, and argument out of range exceptions:
Module Module1 Sub Main() Dim int1 = 0, int2 = 1, int3 As Integer Try int3 = int2 / int1 System.Console.WriteLine("The answer is {0}", int3) Catch e As System.OverflowException System.Console.WriteLine("Exception: Arithmetic overflow!") Catch e As System.ArgumentException System.Console.WriteLine("Exception: Invalid argument value!") Catch e As System.ArgumentOutOfRangeException System.Console.WriteLine("Exception: Argument out of range!") End Try End Sub End Module
If you want to add a general exception handler to catch any exceptions not filtered, you can add a Catch block for the Exception class at the end of the other Catch blocks:
Module Module1 Sub Main() Dim int1 = 0, int2 = 1, int3 As Integer Try int3 = int2 / int1 System.Console.WriteLine("The answer is {0}", int3) Catch e As System.ArgumentOutOfRangeException System.Console.WriteLine("Exception: Argument out of range!") Catch e As System.ArgumentException System.Console.WriteLine("Exception: Invalid argument value!") Catch e As Exception System.Console.WriteLine("Exception occurred!") End Try End Sub End Module
![]() ![]() | ||