I want to stop the program in Java for a few seconds, what should I do?

Asked 2 years ago, Updated 2 years ago, 85 views

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

2022-09-22 22:27

1 Answers

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.


2022-09-22 22:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.