Valhalla Legends Forums Archive | .NET Platform | System.Net.Sockets (Asychronous)

AuthorMessageTime
NicoQwertyu
When trying to close an open connection, manually (Socket.Shutdown() Socket.Close()), wierd exceptions will get thrown in my callback subs (usually in the callback for receive, but sometimes other areas). Am I suppost to put a try/catch statement everywhere I try to use the socket? That just seems messy. :-/

TIA.
April 26, 2007, 12:11 AM
Myndfyr
Try/catch is the appropriate way to do this as Socket calls do not return error codes.
April 26, 2007, 12:36 AM
NicoQwertyu
Thank you.  :)
April 26, 2007, 1:39 PM
Grok
As a side note, Try/Catch is appropriate for nearly everything you write, to gracefully handle unexpected situations without having to crash the user.  It's not a replacement for data validation, logic, and program structure, so don't let us Catch you Trying to use it those ways!  :P
April 26, 2007, 2:09 PM
Myndfyr
[quote author=Grok link=topic=16643.msg168378#msg168378 date=1177596595]
As a side note, Try/Catch is appropriate for nearly everything you write, to gracefully handle unexpected situations without having to crash the user.  It's not a replacement for data validation, logic, and program structure, so don't let us Catch you Trying to use it those ways!   :P
[/quote]

Going on that, published Best Practices indicate that you shouldn't just "catch (Exception ex)" - you should try to be specific about the exceptions you catch according to the ones that can be raised.

Having said so, there are certainly places where it's appropriate to do so.
April 26, 2007, 4:26 PM
NicoQwertyu
So what would be the most appropriate block of code (take this very generally, not just for this example):

[code]
Private Sub DoStuff(ByVal variableOne As String, ByVal variableTwo As String)
        Try
            If variableOne Is Nothing Or variableTwo Is Nothing Then
                '-- Display error code
                Exit Sub
            End If

            '-- DoSomeStuff
            '--
            '--
            '--

            SomeSocketMethod()
        Catch ex As Exception
            '-- Handle Exception
        End Try
    End Sub
[/code]

Or:

[code]
Private Sub DoStuff(ByVal variableOne As String, ByVal variableTwo As String)
        If variableOne Is Nothing Or variableTwo Is Nothing Then
            '-- Display error code
            Exit Sub
        End If

        '-- DoSomeStuff
        '--
        '--
        '--
        Try
            SomeSocketMethod()
        Catch ex As Exception
            '-- Handle Exception
        End Try
    End Sub
[/code]
May 3, 2007, 11:15 PM
Myndfyr
I would stick with the latter.  Sanitizing/validating input is a very good practice to have in order to prevent security holes from cropping up.  Check your design, too, to see if it would be appropriate to throw an ArgumentException (or derived class).
May 4, 2007, 1:36 AM
Imperceptus
I've wondered if you can nest these? Try/catch into a procdure that has another try/catch it?
July 8, 2007, 12:15 AM
squeegee
Isn't there a GetLastError function somewhere you can call to get some data on it?
July 8, 2007, 12:20 AM
Imperceptus
get data on ex? This might be wrong, but have you tried stack trace. Ive found it useful in asp.net and just learned another way to use in in vb.net.  it might have what you need.
July 8, 2007, 1:08 AM

Search