-5

I've heard that I can use another class when I use like that. But I have 2 errors and tried to solve them for several days. Need your help guys. Thank you.

     import java.io.IOException;
     import java.io.InputStream;
     improt java.io.OutputStream;
     import java.net.Socket;
     import java.net.SocketAddress;
     import java.util.logging.Level;
     import java.util.logging.Logger;

     public class motor implements Runnable {
           private static final int final int sizeBuf = 50;
           private Socket clientSock;
           private Logger logger;
           private SocketAddress clientAddress;

     public motor(Socket clntSock, SocketAddress clientAddress, Logger logger) {
           this.clientSock = clntSock;
           this.logger = logger;
           this.clientAddress = clientAddress;
           }

     public void run() {
           try {
                InputStream ins = clientSock.getInputStream();
                OutputStream outs = clientSock.getOutputStream();

                int rcvBufSize;
                byte[] rcvBuf = new byte[sizeBuf];
                while ((rcvBufSize = ins.read(rcvBuf)) != -1) {

                String rcvData = new String(rcvBuf, 0, rcvBufSize, "UTF-8");

                if(rcvData.comparTo("MotorLock") == 0) {
                    Class cls = Class.forName("home/pi/project/servo/servo_close");
                 }
                if(rcvData.comparTo("MotorOpen") == 0) {
                    Class cls = Class.forName("home/pi/project/servo/servo_open");
                 }

            logger.info("Received data :" + rcvData + "(" + clientAddress + ")");
            outs.write(rcvBuf, 0, rcvBufSize);
            }

            logger.info(clientSock.getRemoteSocketAddress() + "Closed");
            } catch (IOException ex) {
                  logger.log(Level.WARNING, "Exception in RcvThread", ex);
            } finally {
                     try{
                         clientSock.close();
                            System.out.println("Disconnected! Client IP :" + clientAddress);
                       } catch(IOException e) {}
                }
             }
         }

And when I compile this code, I've got 2 unreported exception :

      motor.java:35: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
             Class cls = Class.forName("home/pi/project/servo/servo_close");

      motor.java:40: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
             Class cls = Class.forName("home/pi/project/servo/servo_open");
UNESCO
  • 1
  • 1
  • 3
    *Can anyone help me with solving these problems?* I don't think so ... you simply do not understand what is written ... **ClassNotFoundException; must be caught or declared to be thrown** – Selvin Nov 10 '15 at 11:48
  • In your specific case there exists a possibility that the method `Class.forName` will throw a checked exception, and checked exceptions must be caught or declared. – Ceiling Gecko Nov 10 '15 at 11:48

2 Answers2

0

you need to do what the compiler says. Your code throws ClassNotFoundException. This is an Exception and this must be caught by your code or declared as thrown in your method.

So e.g.

 public void foo() {
   try {
      Class cis = Class.forName("...");
   )
   catch(ClassNotFoundException e){
     // log or handle the exception
  }
}

or you can also throw the exception

 public void foo() throws ClassNotFoundException {
   Class cis = Class.forName("...");
 }

but in the latter case the caller of the method has to handle the exception again.

Emerson Cod
  • 1,990
  • 3
  • 21
  • 39
0

To catch all the exceptions you get in your application use:

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

Unless you want to handle multiple exceptions differently.