As we learned in the previous vb exception topic, exceptions are generated by CLR (common language runtime) or application code. To handle runtime or unexpected errors in applications, visual basic has provided a built-in exception handling support by using Try, Catch and Finally blocks.
In visual basic, when an exception is thrown, then the CLR (common language runtime) will look for the Catch
block that handles the exception. In case, if currently executing method does not contain such a Catch
block, then the CLR will display the unhandled exception message to the user and stops the execution of the program.
Following is the syntax of handling errors in visual basic using Try, Catch and Finally blocks.
Try
' Code that may cause exception
Catch ex As Exception
' Exception handling
Finally
' Cleanup resources
End Try
As per the above syntax, the Try
block will contain the guarded code that may cause an exception so that if any errors occurred in our code, immediately the code execution will be moved to Catch
block to handle those exceptions. In case, if no exception occurred in the Try
block, then the Catch
block will be skipped and the execution will be moved to Finally
block.
After completion of Try
or Try & Catch
blocks, the Finally
block will always execute even if an exception occurred or not and it is useful to clean up or dispose the unmanaged objects based on the requirements.
In visual basic, the Try
block must be followed by Catch
or Finally
or both blocks otherwise we will get a compile-time error. In Try-Catch-Finally
statement, only one Try & Finally
blocks are allowed but we can use multiple Catch
blocks to handle different exception types.
Following is the example of handling exceptions in visual basic using Try
, Catch
and Finally
blocks.
Module Module1
Sub Main(ByVal args As String())
Dim name As String = Nothing
Try
If name.Length > 0 Then ' Exception will occur
Console.WriteLine("Name: " & name)
End If
Catch ex As Exception
Console.WriteLine("Exception: {0}", ex.Message)
Finally
Console.WriteLine("Finally Block.")
End Try
Console.ReadLine()
End Sub
End Module
If you observe the above code, we used Try
, Catch
and Finally
blocks to handle runtime or unexpected errors during the execution of the program. Here, we wrote a code that may throw an exception inside of Try
block and in Catch
block we are handling the exception. As discussed, the Finally
block will execute after completion of Try
or Catch
block execution.
When we execute the above code, we will get the result like as shown below.
Exception: Object reference not set to an instance of an object.
Finally Block.
To know more in-detail about exception handling in visual basic, check following exception handling topics.
In visual basic, the Try-Catch
statement is useful to handle unexpected or runtime exceptions that will occur during the execution of the program.
To learn more about the try-catch statement, check Visual Basic Try Catch statement.
In visual basic, Try-Catch-Finally
is useful to handle unexpected exceptions in code. Here, Finally
block is useful to clean up any resources that are allocated in the Try
block.
To learn more about the Try-Catch-Finally statement, check Visual Basic Try-Catch-Finally statement.
In visual basic, Throw
keyword is useful to raise an exception manually and it will throw all the exceptions which are derived from the Exception
base class.
To learn more about Throw keyword in visual basic, check Visual Basic Throw Keyword.