Question

I have a Simple Multithread Server, the code of Threads looks like this:

public void run(){

 while(Ecoute()){

     str=LireTrame(this.socClient);//read text byte by byte 

    //some code here

    Dormir(500);//Sleep for 500ms  
   }     


}

I'd like to create a timeout of 3 minutes, if a client socket does not send a message in 3 minutes, the thread must be closed...

so I try that:

 public void run(){

        try {
        socClient.setSoTimeout(180000);//timeout for 3 min...
    } catch (SocketException ex) {
        Logger.getLogger(tache.class.getName()).log(Level.SEVERE, null, ex);
    }


 while(Ecoute()){

     try{
      str=LireTrame(this.socClient);//read text byte by byte 
    }
    catch(java.net.SocketTimeoutException ex){
     log.append("timeout exception has benn catched \n") ;
     break;//break the while
   } 

    //some code here

    Dormir(500);//Sleep for 500ms  
   }     


}

the problem is that netbeans display an error message: enter image description here

java.net.SocketTimeoutException never thrown in body of corresponding try statement

I tried this with SocketException but same result..

why i can't catch them ?

UPDATE: based on responses from thinksteep and mprabhat, I added th throw in LireTrame(..) but still i can't catch the Exception:

 public  String LireTrame(Socket socClient) throws java.net.SocketTimeoutException{
 int c = 0;
  String Nmea="";
boolean finL=true;
 do{
            try {
                c=socClient.getInputStream().read();
            } catch (IOException ex) {
                Logger.getLogger(tache.class.getName()).log(Level.SEVERE, null, ex);
            }
    char ch=(char)c;

    Nmea=Nmea+ch;
    if(ch==';')
        finL=false;

  }while(finL);
 return Nmea;
 }



mai 11, 2012 8:04:02 PM Daemon.tache LireTrame
Grave: null
java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.net.SocketInputStream.read(SocketInputStream.java:203)
    at Daemon.tache.LireTrame(tache.java:232)
    at Daemon.tache.run(tache.java:87)
    at java.lang.Thread.run(Thread.java:722)

are there any ideas to make this otherwise?

Was it helpful?

Solution

=LireTrame(this.socClient);//read text byte by byte

should throw SocketTimeOutException. You didn't post code for this method, but I am assuming it is not throwing SocketTimeOutException which is why netbeans highlighting.

OTHER TIPS

SocketTimeoutException is a checked exception so before you can catch it you should throw that exception.

So

=LireTrame(this.socClient);//read text byte by byte

should throw SocketTimeoutException before you can catch it.

You are just logging the exception but not rethrowing it

After this line

Logger.getLogger(tache.class.getName()).log(Level.SEVERE, null, ex);

add this line too

 throw SocketTimeoutException('SocketTimeOutException');

I would declare LireTrame() to throw IOException and remove the internal try/catch altogether. It's poorly structured code as it is.

This is admittedly a shot in the dark. I do not use netbeans, I do use eclipse. In eclipse, sometimes JDT compiler gets out of sync with reality. Doing a clean build of the project fixes things up.

I think in your case, it is netbeans that is acting. If you do commandline invocation of javac, do you get that error. If not, doing a clean inside netbeans will very likely fix the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top