Solved

Throw New Exception in Custom Class Library

Posted on 2011-02-17
6
424 Views
Last Modified: 2012-05-11
I have a windows Forms project that references a dataAccess Class Library.

From the windows forms project, I make all data calls to the Class Libary and I want to be able to handle exceptions from the windows forms if an exception occurs in the class library.

I have the code below that I use in my Class Library.

How can I make it so the Windows Forms application will handle an exception.  
I am using "Throw" but windows application still crashes
Public Function FillArcustComboBoxDataTable() As DataTable
        Dim dt As New Data.DataTable

        Dim sqlcmdtext As String = "SELECT ccustno FROM arcust WHERE  cstatus = 'a'"

        Try


            Using SqlConn As New SqlConnection(My.Settings.VAMSQL_ConnString)

                Using da As New SqlDataAdapter(sqlcmdtext, SqlConn)
                    da.Fill(dt)
                End Using
            End Using

        Catch ex As Exception
            'TODO: test exeption
            Throw New Exception(ex.Message)
        End Try

        Return dt
    End Function

Open in new window

0
Comment
Question by:handyjay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34920922
How are you calling this method from your windows app? As long as you are wrapping the call in a Try...Catch block then it should be catching it.
0
 

Author Comment

by:handyjay
ID: 34920980
That is what I was thinking of doing and it does seem to catch it.  I am now wondering if there is a better way to structure my class library.  
Is using "Throw new Exception" in a class library the best way to enable error handeling from calling apps??
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34921093
If you're not doing anything with the exception in your library then there is no real need to catch it, you could just let it bubble up to the caller. If you do catch it, but don't do anything with it, then you are better to just use:
Catch ex As Exception
     Throw
End Try

Open in new window

If you throw a new exception then you lose all of the stack trace information and just throw a new empty exception that won't provide any useful context information to whatever end up catching it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:handyjay
ID: 34922137
So if I need to add the try catch in my windows forms application around each call to the class, then it sounds like I don't really need the Try Catch in the class.

I thought by not having a try catch in the class, and if the class code fails then it would crash the app.  But, if it will bubble up to the windows forms calling app, then I agree and I see no need for it.

I will give this a try tomorrow.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34923879
You'll quite often find that exceptions are caught in libraries and logged, and then get rethrown to the caller to indicate something went wrong. but in your case you aren't doing anything with the exception except rethrowing it, so it is really necessary to catch it.
0
 

Author Comment

by:handyjay
ID: 34926146
Thanks, This works if I dont include a catch in the class as well as if I just use the Throw, so long a I catch it in the calling code.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net 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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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