CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jan 2011
    Posts
    18

    Question Exception Handling Problems

    Whenever I put something within my try-catch blocks, I get this error "cannot find symbol" when I have already defined everything clearly for each try-catch blocks. None of the try catch blocks work, dunno why.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Exception Handling Problems

    Unless you show us the code and the full error message it's hard to say what is wrong other than maybe you haven't declared a variable/method you are using or you haven't added an import statement you need.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Exception Handling Problems

    If this is a duplicate of your other thread then please close one of them or it's going to get really confusing.

    And where's the full error message?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Exception Handling Problems

    client1 is declared inside the try block and so is local to that block. You need to declare client1 outside the try block.

    BTW I should point out you should be closing the socket in a finally block else if any of your code throws an exception the socket won't be closed.

    And learn how to use code tags - read the blue text below this line to see how it is done.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Exception Handling Problems

    I haven't looked - it's hard to read code that hasn't been formatted (hence we keep asking you to use code tags). And I don't have the time at the moment so you'll just have to run it and see if it works.

    Good Luck.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jan 2011
    Posts
    18

    Smile Re: Exception Handling Problems

    Oh nvm tks, will get back 2 u if I face any more probs, tks 4 ur tym.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Exception Handling Problems

    My time-stampping doesn't give any output, dunno why
    You are creating the TimeStamp objects but then not doing anything with them. What are you trying to achieve?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Jan 2011
    Posts
    18

    Lightbulb Re: Exception Handling Problems

    Oh ya, I din realise earlier, was so caught up in fixing my exceptions. Basically, I need time-stamps to check that curr time doesn differ from the time-stamp time by more than 1 min & if it does to retransmit data. I m not sure how to reestablish connection to retransmit data, do I create a new socket for connection re-establishment? Since m implementing a simplified Kerberos protocol, m not sure how to handle the creation of challenges either. Coding on Kerberos seems to be very little on the net, in fact I can hardly find exaamples. If u have knowledge on Kerberos pls share, tks.

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Exception Handling Problems

    Just a small point on exception handling Susan, that will make coding simpler and the finished code clearer...

    The try...catch structure is designed to allow you to write the bulk of your code without worrying about the errors until the end of the method. So you should open a single 'try' block, write all the method code, then catch all the errors at the end, e.g.:
    Code:
    public void aMethod {
       // declare variables that may need tidying up after errors
       ...
       try {
          // initialise variables
          ...
          // put main body of method code here, ignoring exceptions
          ...
          ...
       }
       // now the main work has been done, handle any problems that may have occurred
       catch (ExceptionA a) {
          ...  // handle a
       }
       catch (ExceptionB b) {
          ... // handle b
       }
       finally {
          ... // tidy up variables, close handles, etc.
       }
    } // end of method
    Sometimes you may want to handle an exception and carry on, in which case you could just nest a single try..catch in the body of the code, but the preferred way is to put that piece of code into it's own method so its try..catch is hidden from the main code. This keeps the code body clear of messy exception handling, and ensures all methods follow the same 'single try, multiple catch' formula.

    It's only a guideline, but it is Best Practice

    Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves...
    Alan Kay
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  10. #10
    Join Date
    Jan 2011
    Posts
    18

    Arrow Re: Exception Handling Problems

    I can do that but m trying to fix my socket connection now again, it says my client hasn't been intialized.

    This is my errorneous code

    Client:
    Code:
     Socket client1; //Defined outside
         BufferedReader userInput; //Defined outside
         DataOutputStream ServerOut; //Defined outside
         BufferedReader ServerInfo; //Defined outside
         
         try {
               client1 = new Socket("127.0.0.1",9001);
            } catch (UnknownHostException e) {
                System.err.println(e);
                System.exit(1);
            } catch (IOException e) {
                System.err.println(e);
                System.exit(1);
            }

    Server:

    Code:
     Socket socket1; // Defined outside
     BufferedReader ClientInfo; //Defined outside
    
    ClientInfo =
                   new BufferedReader(new InputStreamReader(socket1.getInputStream()));

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Exception Handling Problems

    If you get an error, please post the full error message and stack trace, if present.

    Incidentally, socket1 on the server has been declared but hasn't been initialised.

    Optimism is an occupational hazard of programming; feedback is the treatment...
    Kent Beck
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  12. #12
    Join Date
    Jan 2011
    Posts
    18

    Arrow Re: Exception Handling Problems

    Ok, I changed my code, now since m sending strings from client to server, I want to know how I can send multiple strings to server. Eg. If client types hello then server echoes hello, then client types world server should echo world & it sholuld continue until client stops so how to send multiple strings in java?

  13. #13
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Exception Handling Problems

    You need the server code to loop back to waiting for the client after it handles the client message, and the client to loop round and send a new message after every server acknowledgement.

    I can't say more without seeing the relevant code.

    Programs must be written for people to read, and only incidentally for machines to execute...
    Abelson and Sussman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  14. #14
    Join Date
    Jan 2011
    Posts
    18

    Re: Exception Handling Problems

    I will post now.

  15. #15
    Join Date
    Jan 2011
    Posts
    18

    Arrow Re: Exception Handling Problems

    Server:

    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class AS3{
        public static void main(String[] args ){
            int i = 1;
            try{
                 ServerSocket s = new ServerSocket(9001);
    
                 for (;;){
                     Socket incoming = s.accept( );
                     System.out.println("Spawning " + i);
                     new RealEchoHandler(incoming, i).start();
                     i++;
                 }
            } catch (Exception e){ System.out.println(e); }
        }
    }
    
    class RealEchoHandler extends Thread{
    	DataInputStream in;
    	DataOutputStream out;
    	private Socket incoming;
        private int counter;
    
        public RealEchoHandler(Socket i, int c){
            incoming = i;
            counter = c;
        }
    
        public void run(){
            try {
    
                in = new DataInputStream(incoming.getInputStream());
    			out = new DataOutputStream(incoming.getOutputStream());
    
                //boolean done = false;
                String str="";
                String [16] store;
                out.writeUTF("Connected!\n");
                out.flush();
                int idx = 0;
                while (idx != 17){
    				out.writeUTF(">");
    				out.flush();
                    str= in.readUTF();
                    System.out.println(in+":"+ str);
                    if (str != null)
                     {
                       store[idx] = str;
                       if(idx % 4 == 0)
                        {
                          out.writeUTF("Echo (" + counter + "): " + str+"\n");
                          out.flush();
            
                        } 
                     }
                      idx++;
                }
    
                incoming.close();
             } catch (Exception e){
                 System.out.println(e);
             }
        }
    
    
    }

Page 1 of 2 12 LastLast

Tags for this Thread

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