Enumeration and Exception Handling in Visual Basic .NET

In this article, I will explain you about Enumeration and Exception Handling in Visual Basic .NET.
  • 2159

Enumeration

Enumeration is used to group related sets of constants. They come in use when we are working with many constants with same type. In visual Basic .NET, we use Enum Keyword to create a Enumeration.

Example:

Imports System.Console
Module Module1
 
    Enum Months
        January = 1
        Feburary = 2
        March = 3
        April = 4
        May = 5
        June = 6
        July = 7
        August = 8
        September = 9
        October = 10
        November = 11
        December = 12
    End Enum
 
    Sub Main()
        WriteLine("March is the " & Months.March & " Month")
        Read()
    End Sub
 
End Module

Output:

Output1.gif
 

Exception Handling

When a program is running and abort without execution that means their are some runtime errors those errors are called Exceptions. Exceptions can be handled by using Exception Handling. We can handle most of the errors that we may encounter and we can enable the application to continue by placing specific lines of code in the application running. In Visual Basic .NET there are two ways to handle exceptions, Unstructured exception Handling which use the on error goto statement andStructured exception handling which use Try....Catch.....Finally statement.

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        Dim X = 0, Y = 1, Z As Integer
        Try
            Z = Y / X
            'The above line throws an Exception
            WriteLine("Z is " & Z)
        Catch E As Exception
            WriteLine(E)
            'Catching the Exception
        End Try
        Read()
    End Sub
 
End Module

Output:

Output2.gif

Summary

I hope this article help you to understand about Enumeration and Exception Handling in Visual Basic .NET 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.