ARTICLE

Exception Handling in VB.NET

Posted by Rohatash Kumar Articles | VB.NET Exception Handling January 19, 2011
This article will show how to use Exception Handling in VB.NET.
 
Reader Level:

An exception is an error which occurred at run time. there are four keyword which are used to find out error at the run time. In Vb.NET is a special type of class called Exception which are used to pass the message .

Try Statement:

Try statement is a block of statement that are used to trap the error at run time. one try statement can have multiple catch statement.

Syntax:

Try...[catch]...{catch|finally}......End Try

Catch Statement:

Catch statement catch the error from try statement and execute the block of statement.

Syntax:

Catch [<exception>[As<type>]] [when<expression>]

Finally Statement:

Finally statement are used to execute some statement irrespective of error. Finally can only one block at the end.

Example: In this example one try statement has more than one catch statement with the final statement and catch statement catch different type of error from try statement.

Module Module1

  

    Sub Main()

        Dim err As Boolean = True

        Dim num As Integer

        Try

 
            Console.WriteLine(" please enter number:")

            num = Integer.Parse(Console.ReadLine())

            Console.WriteLine(" you entered {0}", num)

            err = False

 

        Catch ex As FormatException

            Console.WriteLine("sorry: kindly enter the number only ")

 

        Catch ex As OverflowException

             Console.WriteLine(" number is too large")

 

        Catch ex As Exception

            Console.WriteLine("critical error occurred like copy, paste")

 

        Finally

            If err = False Then

                Console.WriteLine(" thanks for using system")

            End If

        End Try

     End Sub

  End Module

 

Output: Enter the number

op.gif

 Output: when we enter the character in place of number it calls the format exception.

chop.gif

 Output:When we enter the large number than the range of Integer it calls the overflowexception.

ov-op.gif 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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
    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.
Team Foundation Server Hosting
Become a Sponsor