CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2002
    Location
    Seguin, TX / Long Island, NY
    Posts
    16

    recovering from exceptions C#

    Hello ... I'm writing a Client/Server socket program in which all clients get passed a date/time string for a fixed amount of time. I have the write() function in a try {} block and following that is the exception block (SocketException). The problem I'm having is when the client closes its connection before the loop can terminate it causes the program to throw exceptions - which it should.

    What I'm looking for: I'm looking for a way to handle a recovery from the current exception in this case I know its a SocketException because of the write() function. (i.e. in this case reconnect or give an error message, then close down all TCP connections and quit ). I was looking into delegates and events to handle this but now I'm going alittle crazy!!

    If anyone has any examples of recovering from an exception that has occured or another error routine it would help a bunch!!

    Thanks - Fred

  2. #2
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    I would do something like this(that is, if I understand your problem correctly):
    Code:
    while( someCondition )
    {
       try
       {
           //do funky socket stuff here
       }
       catch ( AppropriateException ex )
       {
          //deal with the exception - log it, display a message or just ignore it
       } 
    }
    Now whenever the exception is thrown, you deal with it in the catch block and the program resumes execution at the top of the while block.

  3. #3
    Join Date
    Jul 2002
    Location
    Seguin, TX / Long Island, NY
    Posts
    16
    But I want to deal with the error, the catch statement does nothing for me, all it does is say what happened. Can you even have code in a catch { } that does anything?

    Are there alternativea besides try {} and catch {} ... an if statement to check all client connections in a while loop when Im writing to them .. maybe in a seperate thread?

  4. #4
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    Originally posted by FredoSP
    But I want to deal with the error,
    Then do that in the catch statement. That's what it's for.
    Can you even have code in a catch { } that does anything?
    You can have whatever you want in a catch block.

    Are there alternativea besides try {} and catch {} ... an if statement to check all client connections in a while loop when Im writing to them .. maybe in a seperate thread?
    Sounds like a concurrency nightmare to me.

  5. #5
    Join Date
    Jul 2002
    Location
    Seguin, TX / Long Island, NY
    Posts
    16
    Originally posted by Arild Fines

    Then do that in the catch statement. That's what it's for.

    You can have whatever you want in a catch block.


    Sounds like a concurrency nightmare to me.

    NOPE! For some reason it wont even print out anything in the catch block .. .for example ...

    catch( SocketException )
    {

    System.Console.WriteLine("Got Here");
    }

  6. #6
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    Did you actually verify this in the console window, or did you just step through it in the debugger? There is a known debugger bug() that seems to manifest itself with certain control structures.
    Anyway, this works just dandy for me:
    Code:
    class Class1
    {
    	/// <summary>
    	/// The main entry point for the application.
    	/// </summary>
    	[STAThread]
    	static void Main(string[] args)
    	{
    		try
                   {
                       throw new Exception();
                   }
                   catch( Exception )
                   {
                       Console.WriteLine( "Wheeee!" );
                   }
    	
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured