有关异常的错误是什么错误

时间:2016-03-19 01:22:42

标签: java

public class myclient
{

    public static void main(String argv[]) throws Exception
    {
        int portNum = 11234;  //default port number, remember to change yours
        String servIP="147.97.156.237";  //default Ip


        if (argv.length > 1)
                servIP = argv[1];  //get the IP

        String sentence;
        String modifiedSentence;
        Socket clientSocket = null;
        try {
            clientSocket = new Socket(servIP, portNum);

            Scanner inFromSocket =
               new Scanner(clientSocket.getInputStream());
            PrintWriter outToServer =
               new PrintWriter(clientSocket.getOutputStream(),true);
            //Set up stream for keyboard entry...
            Scanner inFromUser = new Scanner(System.in);

            System.out.print("Enter message: ");
            sentence = inFromUser.nextLine();
            outToServer.print(sentence);
            outToServer.flush();

                        modifiedSentence = inFromSocket.nextLine();
                        System.out.println("FROM SERVER: " + modifiedSentence);
        }
        catch(IOException ioEx)
        {
           System.out.println("Exception ");
        }

           try{
            clientSocket.close();
        }
           catch(IOException e)
           {
               System.err.println("could not close port");
               System.exit(1);
           }

    }
}

IT是一个尝试与服务器通信的客户端程序,但有一些异常错误,即使包含了所需的java包,也可以看到错误

1 个答案:

答案 0 :(得分:2)

这是一些糟糕代码:

catch(IOException ioEx)
{
   System.out.println("Exception ");
}

这告诉你没有。至少打印堆栈跟踪。

catch(Exception e) {
   e.printStackTrace();
}