Custom Exception Handling Issue

sevenhalo

Well-known member
Joined
Oct 21, 2005
Messages
137
Location
Iowa
Programming Experience
3-5
I have a namespace called "NetworkComponents" of various tools I'm using for network awareness. One of the classes in Network is called Host. It's a custom object I'm using to resolve a Machine Name or an IP Address to a manageable object.

VB.NET:
Namespace NetworkComponents
   Class Host
      Private _Host As System.Net.IPHostEntry '--The main element being used
      Private _LocalHost As Boolean = False '--Constructed image from localmachine
      Private _IPArray() As System.Net.IPAddress '--Array of IP Addresses associated with _Host
#Region "Constructor"
      Public Sub New(Optional ByVal strHost As String = "")
      '''Will accept either Machine Name or IP of the Host to construct
      Try
         'A blank value indicates that they want Local as the host
         If strHost <> "" Then
            Dim RE As New System.Text.RegularExpressions.Regex("((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)")
            If RE.IsMatch(strHost) Then
               'strHost is an IP Address in string notation
               _Host = System.Net.Dns.Resolve(strHost)
            Else
               'strHost is a name of a Machine
               _Host = System.Net.Dns.Resolve(strHost)
            End If
         Else
            'Construct based on local computer
            _Host = System.Net.Dns.Resolve(System.Net.Dns.GetHostName)
            _LocalHost = True
         End If
      Catch ex As Net.Sockets.SocketException
         'DNS Exception, name/IP did not resolve
      Catch ex As ArgumentException
         'Something slipped, whatever they passed didn't look like either notations
      End Try
   End Sub
#End Region
When I construct an object of type NetworkComponents.Host, I'm attempting to validate through DNS as well. The problem I'm running into is, if a Net.Sockets.SocketException occurs, how can I throw an exception back to the method creating the object to let it know that DNS didn't resolve? This is what I'm basically attempting to do:

VB.NET:
Private Sub btnTestCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestCmd.Click
   Try
      Dim test As New NetworkComponents.Host(Me.txtHostBox.Text.Trim)
   Catch ex As NetworkComponents.DNXException
      'Notify User of invalid Host
   End Try
End Sub
 
Youare looking for "Throw"

VB.NET:
Private Sub btnTestCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestCmd.Click
        Try
            Dim test As New NetworkComponents.Host(Me.txtHostBox.Text.Trim)
        Catch ex As NetworkComponents.DNXException
            'Re throw the original exception to the calling code
            Throw

            ' Throw a new Exception to the calling code (should be more specific than just Exception as I have listed below)
            Throw New Exception("Message of Exception", ex)
        End Try
    End Sub
 
Thanks for the reply!

VB.NET:
Catch ex As NetworkComponents.DNSException

This is actually the problem. The second peice of code is just a random GUI I threw together to test the NetworkComponents namespace. It isn't going to throw the exception, but handle it when NetworkComponents throws the DNSException.

The problem I'm having is that all the exceptions I throw in NetworkComponents (custom, system, application, etc.) are stuck to only being handled locally. I'm attempting to throw the exception so that the referencing component (in this case, the form that has btnTestCmd on it) will catch it, but can't figure that out.

Basically, the code above is what I'm trying to accomplish when all is said and done.
 
The first code sample I gave is the contructor for the host object I'm creating (it's going to be part of the business tier and used in multiple applications). The second example is an appliction that references it and uses the object.

If the parameters given to the constructor are not correct (they do not resolve); I need for it to throw an exception (in this case, I'm creating a custom exception "DNSException) to the application using the object.

The first example will catch a socket exception if the DNS does not resolve. Where it says "'DNS Exception, name/IP did not resolve" is where I need to throw another cutome exception named "DNSException." The problem I'm running into is that the second example won't catch the custom exception (ie. NetworkComponents.DNSException is not defined in the form object with btnTestCmd).

I hope that's a little more clear? :(

Thanks for the help thus far.
 
Schenz,

Thanks for the help; I figured this out. I was able to have the referencing components handle things like this with delegates. :)
 
Back
Top