Raising Events and Throwing Exceptions

I'm creating a custom class and I need some of my methods to raise events or throw custom exceptions. How would I go about doing this? What are the best practices for this? I want everything self-contained in my custom class. Is this possible or advisable?
xmlnewbieAsked:
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.

RonaldBiemansCommented:
small example
Public Class mytest
    Public Event myevent(ByVal Text As String)

    Public Sub myrun()
        For x As Integer = 1 To 10
            'whatever
        Next
        RaiseEvent myevent("finished")
    End Sub
End Class

Private Sub myeventhandler(ByVal s As String)
        MsgBox(s)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim y As New mytest
        AddHandler y.myevent, AddressOf myeventhandler

        y.myrun()
    End Sub
xmlnewbieAuthor Commented:
1. So, I can declare these events in the class that I am making?

2. Where is myeventhandler supposed to be placed?

3. Can I really expect people using my class to add "AddHandler y.myevent, AddressOf myeventhandler" to their code just to catch my events? Can't it be raised and handled similar to how, say, the Button1.Click event is raised and handled?

4. What about custom exceptions? Do I need to create a totally different class for that??? That would be odd to me since I also don't think I can expect people using my class to make sure they have my custom exception class.
xmlnewbieAuthor Commented:
Ronald?
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

RonaldBiemansCommented:
Sorry xmlnewbie, Completly forgot about this question :-),

I haven got much time today (I have to work too :-))  but look at this site it explains very well how to build controls (exposing events, properties etc)

http://www.dotnet247.com/247reference/a.aspx?u=http://www.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx
xmlnewbieAuthor Commented:
Thanks. I'll look at the link. My concerns are stated in a previous post. Also, my question deals with throwing custom exceptions. Any guidance on that?
xmlnewbieAuthor Commented:
Ronald, Any guidance on throwing exceptions? I'm still studying the link on raising events. Thanks.
RonaldBiemansCommented:
consider this example

Public Class Class1

    Sub New()

    End Sub

    Public Function dividing(ByVal A As Integer, ByVal B As Integer) As Double
        If B = 0 Then
            Throw New MyException("cannot divide by zero")
        End If
        Return A / B
    End Function

    Public Class MyException
        Inherits System.ApplicationException
        Public Sub New()
        End Sub
        Public Sub New(ByVal Message As String)
            MyBase.New(Message)
        End Sub
        Public Sub New(ByVal Message As String, ByVal Inner As Exception)
            MyBase.New(Message)
        End Sub
    End Class

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As New Class1
        Try
            x.dividing(10, 0)
        Catch ex As Class1.MyException
            MsgBox(ex.Message())
        End Try
    End Sub

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
xmlnewbieAuthor Commented:
Ohhhhhhhhhhhh. Now, throwing exceptions makes sense to me. I can make the exception class part of the custom class that I am developing. Meaning, I don't need to have my class users use another separate class just to catch my exceptions.

Still going thru your link.
xmlnewbieAuthor Commented:
What are you supposed to do with Inner in:

Public Sub New(ByVal Message As String, ByVal Inner As Exception)
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.