Solved

Throw New Exception in Custom Class Library

Posted on 2011-02-17
6
419 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:handyjay
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

12 Experts available now in Live!

Get 1:1 Help Now