简体   繁体   中英

“Unreported exception” from method call despite catching esception

I recieve 3 errors in the following section of code:

public static void main(String[] args){

     CS_Socket cs = new CS_Socket();

     try{
         new Thread(() -> cs.EchoServer()).start();
         new Thread(() -> cs.EchoClient()).start();
       }

     catch(IOException e){

       System.out.println(e.getMessage());

     }
}

For both new threads, I receive unreported exception java.io.IOException; must be caught or declared to be thrown unreported exception java.io.IOException; must be caught or declared to be thrown as a compiler error.

In catch(IOException e) , I receive exception java.io.IOException is never thrown in body of corresponding try statement .

I'm new to Java and have no idea how to fix these errors. None of the other questions about these errors seem to contain answers relevant to my code.

Any help is greatly appreciated.

Exception thrown in a thread normally couldn't be caught in another thread.

See this link for more explanation How to catch an Exception from a thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM