SiteMap  | About    

Custom Exceptions in VB.NET

Visual Basic .NET offers structured exception handling that provides a powerful, more readable alternative to "On Error Goto" error handling, which is available in previous versions of Microsoft Visual Basic.

In VB.Net we can handle exceptions with great ease and we can also create our own customized exceptions which can later be used for our applications specific needs. Exceptions are objects that encapsulate an irregular circumstance, such as when an application is out of memory, a file that cannot be opened, or an attempted illegal cast.

You can also throw an exception from within your own code using the keyword Throw.

The following VB.NET program shows, how to create a custom exception class and how it is using in the program.

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
			Dim i As Integer
			Dim j As Integer
			Dim k As Integer
			j = 10
			k = 0
			i = j / k
		Catch ex As Exception
			Throw (New MyCustomException("You can not divide a number by zeo"))
		End Try

	End Sub
End Class

Public Class MyCustomException
	Inherits System.ApplicationException

	Public Sub New(ByVal message As String)
	End Sub

End Class (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.