ARTICLE

Custom exception handling in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic 2010 October 01, 2010
This article gives you a idea about exception handling in Visual Basic.Net and also how you can create your own custom exceptions.
 
Reader Level:

In this article you will learn How to create custom exception in VB.NET.

Creating Custom Exceptions: VB.NET define your own custom exceptions to identify the occurrence of unexpected events in your code.exceptions are implemented in VB.Net as classes and objects.

  1. Create a exception kind of class and inherit it from Exception class.

  2. Override the Message property and ToString() method.

For example:

 

Module Module1

    Class NotEnoughBalanceException

        Inherits Exception

        Public Overrides ReadOnly Property Message() As String

            Get

                Return "Sorry! Not Engouh Balance"

            End Get

        End Property

    End Class

    Class Customer

        Private name As String

        Private balance As Integer

        Public Sub New(ByVal name As String, ByVal opamt As Integer)

            Me.name = name

            Me.balance = opamt

        End Sub

        Public Sub Deposit(ByVal amount As Integer)

            balance += amount

        End Sub

        Public Sub Withdraw(ByVal amount As Integer)

            If amount > balance Then

                Throw New NotEnoughBalanceException()

            End If

            balance -= amount

        End Sub

        Public Sub ShowBalance()

            Console.WriteLine("Balance of {0} is {1}", name, balance)

        End Sub

    End Class

    Class ExpTest

        Public Shared Sub Main()

            Dim c As New Customer("Amit", 5000)

            Try

                c.Deposit(4000)

                c.ShowBalance()

                c.Withdraw(20000)

                c.ShowBalance()

            Catch ex As NotEnoughBalanceException

                Console.WriteLine(ex.Message)

            End Try

 

        End Sub

    End Class

 End Module


 OUTPUT:

ex.gif

 Conclusion:

Hope this article would have helped you in understanding custom exception handling in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
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.
    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.
Nevron Diagram
Become a Sponsor