ARTICLE

Enumeration and Exception Handling in Visual Basic .NET

Posted by Atul Chaudhary Articles | Visual Basic 2010 January 02, 2011
In this article, I will explain you about Enumeration and Exception Handling in Visual Basic .NET.
 
Reader Level:

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

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 and Structured 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.  

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor