User Define Exception in VB.NET

In this article we will discuss about the User Define Exception in VB.NET
  • 8088

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.


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)
        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()
                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()) 
            'independent catch block for the user define Exception
        Catch ex As Insuficentfund
            Console.WriteLine("***Exception in Bank Account Blance***")  
       End Try
    End Sub


© 2020 DotNetHeaven. All rights reserved.