ARTICLE

How to create Custom exception handling in VB.NET

Posted by Rohatash Kumar Articles | VB.NET Exception Handling January 21, 2011
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.

Exception

Exceptions are the occurrence of some condition that changes the normal flow of execution .

For example:

your program run out of memory , file does not exist in the given path , network connections are dropped etc. More specifically for better understanding , we can say it as Runtime Errors .

The Concept

The whole idea of having customized Exception Handling is centered around the fact that there needs to be a generic approach of catching and throwing Exceptions.

For implementing custom Exception Handling we need to derive the class CustomException from the system base class ApplicationException. In general, for customizing Exception Handling the following components are necessary:

1. A custom exception class which would host the exception thrown and can be thrown as a new exception.
2. A message class that would host all user - friendly Application messages that need to be displayed.

 Creating Custom Exception


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
 
Team Foundation Server Hosting
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.
    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.
Nevron Diagram
Become a Sponsor