try, catch and finally in Vb.net

Most programs cannot anticipate every possible error. In cases where you cannot predict all possible errors, you can use a try catch block. Place the code that could fail inside the try part. Then use one or more catch sections to handle any errors that occur. An optional exception variable can gather information about the error for your catch code to use in determining what went wrong and what you should do about it.
The following code shows how the program performs the calculation.

  Private Sub Calculation()
         performCalculation("abc", "45")
         performCalculation("45", "0")
  End Sub

When we call performCalculation function with “abc” and “45” values  then “abc” cannot be parse into integer and it goes to format exemption , and when we pass the value “45” and “0” then following  expression gives the error ‘Arithmetic operation resulted in an overflow:

     Dim result As Integer = x / y (result=45/0)

Private Sub PerformCalculation(ByVal xStr As String, ByVal yStr As String)
 
        Dim resultStr As String = ""
        Try
            Dim x As Integer = Integer.Parse(xStr)
            Dim y As Integer = Integer.Parse(yStr)
            Dim result As Integer = x / y
            resultStr = result.ToString()
 
        Catch fmt_ex As FormatException
            MessageBox.Show( _
                "The input values must be integers.", _
                "Invalid Input Values", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Calculation Error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
            Console.WriteLine(ex.GetType().Name)
        Finally
            MessageBox.Show("Calculation complete", "Calculation complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.