Solved

Throw New Exception in Custom Class Library

Posted on 2011-02-17
6
421 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

816 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

10 Experts available now in Live!

Get 1:1 Help Now