I wanted to stop the program, so I tried methods like delay or sleep but I couldn't use it because of the error below.
unreported exception java.lang.InterruptedException; must be caught or declared to be thrown.
These error messages come out when you do Thread.sleep(x) or wait(). Is there anything I need to do before I use the above two methods?
java sleep
If you read the error message carefully, you can see what the problem is. The compiler did not make any exceptions to threading and thread interruption in the code above, so I sent an error message.
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} } catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
You can do it like this.
© 2024 OneMinuteCode. All rights reserved.