How to throw an exception in a class library

Posted on 2005-05-12
Last Modified: 2010-04-23
I have 2 functions in a class library performing various things. I will make use of this dll in a form by instantiating it and calling the functions. How can I introduce a try catch block in my functions to throw any error that occurs if an error is encountered. I know you throw exceptions in dll's but not sure if I have to use the new keyword.


public function GetLastName(byval lastname as string)

--- body of the code

catch (ex as exception)

End function

Then, How will I handle this in my form calling this function.


Question by:Sirdots
    1 Comment
    LVL 4

    Accepted Solution

                'code body
            Catch ex As Exception
                Throw New Exception(ex.Message)
                Exit Function
            End Try

    in the code you will have this to access your method (assume this method is located in class ClassName in the library)

    Dim c As New ClassName
    Dim lastName as string
        Label1.Text = c.GetLastName(lastName)
    Catch ex As Exception
        'Error handling
    End Try

    if an error occurs in your library it should toss the error up until you catch it and deal with it or the error comes out outside a try catch.  I hope this helps.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now