ARTICLE

Exception in VB.NET

Posted by Munesh Sharma Articles | VB.NET Articles March 28, 2011
In this article we will discuss about the Exception
Reader Level:

Exception:

Exception is an event, that effect the normal flow of code during the program execution. Exception is divided in to two parts:

  • System define Exceptions

  • User define Exception

System Define Exception: 

These Exceptions are created by the Microsoft and are considered to be part of the .NET Framework and FCL. by the convention ,system define Exception Inherit from the System Exception class. There are various type of system Exceptions that are:

  • NullReferance Exception 

  • InvalidCast Exception

  • IndexOutofRange Exception

  • Invalid Exception

  • TypeLoad Exception

  • ExecutionEngine Exception

  • OutOfMemory Exception

  • StackOverFlow Exception

  • Arithmetic Exception   

  • OverFlow Exception

  • DividebyZero Exception

  • NotFiniteNumber Exception

  • Argument Exception

  • ArgumentNull Exception

  • ArgumentOutOfRange Exception 

User Define Exception:

Some times you need to report an error condition that does not map to an existing system define exception classes. In such case you can design a custom exception class for creating  and throwing user defined exceptions by creating a new class that Inherit from Application Exception class.

Exception Handling:

In .NET Framework Exception handling is done by the three blocks. That are:

  • Try

  • Throws

  • Catch or Finally

Try Block:

A Try Statement allow you to perform operations and to call other method within a try block. the try block is often referred to as a guarded block because it give the CLR an associated backup plan for what to do when things go wrong.

Module Module1 
    Sub Main()
        Try
            Dim x As Integer
            Dim y As Integer
            x = 123
            y = x / 0   
        Catch ex As Exception
            Console.WriteLine("DividebyZero Exception")  
        End Try
 
    End Sub
 
End
Module

Throws Black:

Throwing an exception is the one an only official way to report an error condition to the .NET Framework. if you call a method and it does not throw an exception, you can assume that the method was able to do what you expected it to do.

Catch Block: 

In Catch Block, it can hold the exception event that are occur during program execution time.

Catch ex As Exception

Coding for System Define Exception Handling:

Module Module1
    Class over
        Dim value As Integer = 980000000 
        Public Sub voer1()
            Try
                ' Square the original value.
                Dim square As Integer = value * value
                Console.WriteLine(value, square)
            Catch e As OverflowException
                Dim square As Double = Math.Pow(value, 5)
                Console.WriteLine("Exception", _
                                 square, Int32.MaxValue)
            End Try
 
        End Sub
    End Class
 
   Sub Main()
        Dim obj As New over
        obj.voer1()  
    End Sub
 
End
Module

Output:

  Output-of-exception.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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor