Exception Handling using VB.NET

In this small article you will learn how to use Exception Handling in visual Basic.NET
  • 2376

Exception handling is a technique of handling a situation which is occur at the run time when program abort without execution and the cause of that situation is called exception.

Exception can be handled by two ways in VB.NET .

1.Structured exception handling.
2. Unstructured exception handling.

Hear I am going to tell about the structured exception handling using : Try….Catch….Finally.

Example

Module Module1

    Sub Main()
        Dim a = 0, b = 1, c As Integer
        Try
            c = b / a
            Console.WriteLine("C is " & c)
        Catch e As Exception
            Console.WriteLine(e)
            Console.WriteLine("0 can't be divided by any number")
            Console.ReadLine()
        End Try

    End Sub

 End Module

Output

6.gif
 

In this code a numbers divided by zero which result is infinity. This error is thrown by Try Block and Catch block shows the exception after catching it.

Conclusion

This example helps you to understand how exception occurs than thrown and than catch and shows the exception at the run time.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.