VB.net exception class

I am trying to use the exception class as coded below to create a new exception with a specific error number, I thought reading the descriptor of HRESULT that I would be able to set the value using a '=', however it throws an error when I do this.  My next step was to implement a partial class that overloads the property for HRESULT, however I obviously either did that wrong, or am trying to use the partial class in a way that it is not meant to.  So, How can I Set a custom Exception, with a custom error number in VB.net?

Dim ex As New Exception("Error message!")
ex.HResult = Int(10001981)

Open in new window

Partial Class

Partial Class exception
    Inherits System.Exception
    Dim hresult_holder
    Public Overloads Property HRESULT As Integer
            Return hresult_holder
        End Get
        Set(value As Integer)
            hresult_holder = value
        End Set
    End Property
End Class

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
You cannot create a Partial class from a class that is already compiled. Partial is used to combine source code from 2 files when you are compiling. It works only if you have the source code.

HResult are usually reserved for system errors. Along with the error code, they also provide information about the severity of the problem and where it comes from.

What you need to do is to create your own exception class by inheriting from Exception, and add your own property to it.
Public Class MyException
	Inherits System.Exception

	Private _ErrorCode As Integer

	Friend Sub New(errorCode As Integer)
		_ErrorCode = errorCode
	End Sub

	Public ReadOnly Property ErrorCode As Integer
			Return _ErrorCode
		End Get
	End Property

End Class

Open in new window

When you need to trigger that exception, simply use code like the following:
Throw New MyException(12345)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RCMiller1981Author Commented:
That is what I feared, This solution was my backup, and now my primary....

Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.