ARTICLE

User Define Exception in VB.NET

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

User define Exception:

The .NET Framework provide a hierarchy of Exception classes ultimately from the  base class Exception. so in many case you only have to catch the exception. You can create your own exception classes is known as user define exception.

                             User-define-Exception.gif

Benefits of User Define Exception:

  • You can Design exception object that contain application specific error information.

  • User define exception object can be trapped and handled separately in their own independent catch block.

Coding For User Define Exception:  

Module Module1
    'User define Exception class.
    Public Class Insuficentfund : Inherits Exception
        Sub New()
            MyBase.New("error occurred due to insuficent funds")  
        End Sub
        Sub New(ByVal msg As String)
            MyBase.New(msg)  
        End Sub
        Sub New(ByVal msg As String, ByVal inner As Exception)
            MyBase.New(msg, inner)  
        End Sub
    
    End Class
    Public Class Bank  
        Public Sub withdraw1(ByVal amount As Integer)
            If (amount < 0) Then
                ' Use the user define Exception Class
                Throw New Insuficentfund()
            Else
                Console.WriteLine("Welcome to Bank Account")
                Console.WriteLine("Your bank Account Balance is = " & amount)   
            End If
        End Sub
 
    End Class
   
    Sub Main()
        Dim obj As New Bank
        Console.WriteLine("if you enter =Ve value exception is Occurr")
        Dim x As Integer
        x = Int32.Parse(Console.ReadLine()) 
        Try
  
            obj.withdraw1(x)
            'independent catch block for the user define Exception
        Catch ex As Insuficentfund
            Console.WriteLine("***Exception in Bank Account Blance***")  
       End Try
    
    End Sub
 
End
Module


Output:

          Output-of-User-Define-Excep.gif

 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor