Results 1 to 2 of 2

Thread: Socket problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321

    Question Socket problem

    If anyone has any suggestions on why I am recieving erros with this code, Id be gratefull.

    Code:
    import java.io.*; 
    import java.net.*; 
    
    
    class MyConnectionHandler implements Runnable{
          String clientSentence; 
          String capitalizedSentence; 
          Socket S;
    
    	public MyConnectionHandler(Socket s){
    		S = s;
    	}
    
    	public void run(){
               BufferedReader inFromClient = new BufferedReader(new InputStreamReader(S.getInputStream())); 
    
               DataOutputStream  outToClient = new DataOutputStream(S.getOutputStream()); 
    
               clientSentence = inFromClient.readLine(); 
    
               capitalizedSentence = clientSentence.toUpperCase() + '\n'; 
    
               outToClient.writeBytes(capitalizedSentence); 
    	}
    }
    The errors are:

    > cd C:\Java\1
    > c:\j2sdk1.4.1_01\bin\javac MyConnectionHandler.java


    C:\Java\1>c:\j2sdk1.4.1_01\bin\javac MyConnectionHandler.java
    MyConnectionHandler.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(S.getInputStream()));
    ^
    MyConnectionHandler.java:17: unreported exception java.io.IOException; must be caught or declared to be thrown
    DataOutputStream outToClient = new DataOutputStream(S.getOutputStream());
    ^
    MyConnectionHandler.java:19: unreported exception java.io.IOException; must be caught or declared to be thrown
    clientSentence = inFromClient.readLine();
    ^
    MyConnectionHandler.java:23: unreported exception java.io.IOException; must be caught or declared to be thrown
    outToClient.writeBytes(capitalizedSentence);
    ^
    4 errors
    Thanks for any help you can offer

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Several functions you call might throw an IOException. Java requires that you catch or report all exceptions not derived from RuntimeException.

    So either
    Code:
    void func()
    {
      try {
        obj.funcThatMightThrow();
      } catch(IOException e) {
        e.printStackTrace();
      }
    }
    or
    Code:
    void func() throws IOException
    {
      obj.funcThatMightThrow();
    }
    Note that the second doesn't solve the error, it only delays it. The function that calls your function would have to catch the exception if yours doesn't.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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