Solved

Throw New Exception in Custom Class Library

Posted on 2011-02-17
6
428 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 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