VB.NET Exception Handling

  • An exception can be considered as an error which is caused due to a run time error in a program.
  • When the compiler encounters with the error such as dividing by zero, it creates an exception object and informs us that an error has occurred.
  • If we want that the program remains continue executing the remaining code, we have to Catch the exception object thrown by the error condition and display an appropriate message for taking corrective action. This process is known as the exception handling.
  • If we do not Catch and handle the exception object properly the compiler will display an error message and will terminate the program.

In VB.NET 2010 Exception can be divided into two parts:

Exception handling

We will discuss these Exceptions in details later, now we are going to discuss some important terms regarding exception handling:

  • Try block: The code that may be throw exception which we want to handle is put in the Try block. It is followed by one and more Catch block.
  • Catch block: The code to handle the thrown exception is put in the Catch block just after the Try block.
  • Finally block: Finally block is always executed whether the exception is thrown or not by the Try block.
  • Throw statement: Any method can throw the exception using Throw statement when any unexpected event is occur.

Standard Exceptions

  • The exceptions which are pre-defined and provide us with the help of some classes called Exception classes by .NET Class Library are known as Standard Exceptions.
  • These Exception classes are defined in the System namespace.
  • Some of the exception classes present in .NET class library are given below in the table:
  • .
Exception handling

Handling Standard Exceptions using Try-Catch block
  • The piece of code to handle the exception is written in the Try-Catch block.
  • When a program starts executing, the program control enters the Try block. If all the statements present in Try block successfully executed, then the program control leaves the Try-Catch block.
  • If any statement fails to execute, the execution of remaining statements is stopped and the program control goes to the Catch block for handling the exception.
Some facts about Try-Catch block
  • A Try block must be followed by a Catch block.
  • A Try block can have multiple Catch blocks.
  • The Try block must lie inside a method.

Here is an example of handling exceptions using Try-Catch block:

  • Open Microsoft Visual Studio 2010.
  • Click on File|New Project option. A New Project dialog box will appear.
  • Now select Visual Basic language and Windows Form Application from the pane and named the application as Exceptions.
  • Now add two TextBox controls and a Button control on the form from the ToolBox.
  • Change the Text property of Button1 control to Divide.
  • Double click on the Form to open its code window and add the following code in it:
  • Public Class Form1
    Public Sub Divison(ByVal x As Integer, ByVal y As Integer)
    Dim z As String
    x = x / y
    z = Convert.ToString(x)
    Catch de As DivideByZeroException
    MessageBox.Show("Cannot divide by zero." + "This is a Logical Error" & de.ToString())
    End Try
    End Sub
    End Class

  • Now double click on the Divide button and add the following code in its code window:
  • Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim p, q As Integer
    p = Convert.ToInt32(TextBox1.Text)
    q = Convert.ToInt32(TextBox2.Text)
    Divison(p, q)
    End Sub
    End Class

  • Run the application by pressing F5 key. Now this will shows a form as given in the following diagram:
  • Exception handling

  • Now enter 10 in first textbox and 0 in second textbox this will shows the following result:
  • Exception handling

User-Defined Exceptions

  • Sometimes a situation is arises that we have to handle an exception for which there is no exception class defined in the .NET class library.
  • In that cases we are called the classes as user-defined classes and the exception handled by these classes are known as user-defined exceptions.

    Handling User-Defined Exceptions

    Here is an example which shows the handling of user-defined exceptions:

    • Open Microsoft Visual Studio 2010.
    • Click on File|New Project option. A New Project dialog box will appear.
    • Now select Visual Basic language and Console Application from the pane and named the application as UserDefinedExceptions.
    • Now add the following code in code window:
    • Module Module1
      Sub Main()
      Dim x, y As Integer
      x = 3
      y = 15000
      Dim z As Single
      z = Convert.ToString(x / y)
      If z < 0.001 Then
      Throw New myexception("number is very small")
      End If
      Catch ex As myexception
      Console.WriteLine("exception caught")
      Console.WriteLine("Control entered in Finally block")
      End Try
      End Sub
      Class myexception
      Inherits Exception
      Public Sub New()

      End Sub
      Public Sub New(ByVal message As String)
      End Sub
      Public Sub New(ByVal message As String, ByVal inner As Exception)
      MyBase.New(message, inner)
      End Sub
      End Class
      End Module

    • Now run the application by pressing F5 key this will produce following output:
    • Exception handling