Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Throw New Exception in Custom Class Library

Posted on 2011-02-17
6
Medium Priority
?
432 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 2000 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
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!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

610 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