How do I fix a compilation error for unhandled exception on call to Thread.sleep()?
Thread.sleep can throw an InterruptedException which is a checked exception. All checked exceptions must either be caught and handled or else you must declare that your method can throw it. You need to do this whether or not the exception actually will be thrown. Not declaring a checked exception that your method can throw is a compile error.
You either need to catch it:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
// handle the exception...
// For example consider calling Thread.currentThread().interrupt(); here.
}
Or declare that your method can throw an InterruptedException
:
public static void main(String[]args) throws InterruptedException
Related
- Lesson - Exceptions
- When does Java's Thread.sleep throw InterruptedException?
- Java theory and practice: Dealing with InterruptedException
Error even after throws Exception for thread.sleep
Because you calling Thread.sleep inside foreach, Below will solve your issue:
public void setUp() throws InterruptedException {
List<String> simulatorList = new ArrayList<>();
for (String s : simulatorList) {
Thread.sleep(1000 * 60 * 1);
}
}
Thread.sleep won't work unless it is wrapped in a try catch block in Selenium
The Thread.sleep() method throws an InterruptedException. Whether or not this exception will actually get thrown is dependent on what happens during the execution of your java code, the method is just letting you know it could happen, and that you should handle it in some way.
One way to handle the exception is to put it inside a try catch block, so if the exception gets thrown, the program will still continue and the code inside the catch block will execute.
If you really don't want a try catch block (no idea why you wouldn't) you can add a throws declaration at the top of your method which would look something like this:
public void test() throws InterruptedException {
I would read up some more about java exceptions and how they work
https://stackify.com/specify-handle-exceptions-java/
https://www.geeksforgeeks.org/exceptions-in-java/
I get exception when using Thread.sleep(x) or wait()
You have a lot of reading ahead of you. From compiler errors through exception handling, threading and thread interruptions. But this will do what you want:
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
sleep from main thread is throwing InterruptedException
What you see is a compilation error, due to the fact that you didn't handle the checked exception (InterruptedException
in this case) properly. Handling means doing one of the following:
1) Declaring the method as throws InterruptedException
, thus requiring the caller to handle the exception
2) Catching it with a try{..}catch(..){..}
block. For example:
try {
Thread.sleep(1500);
} catch(InterruptedException e) {
System.out.println("got interrupted!");
}
InterruptedException
is used to indicate that the current thread has been interrupted by an external thread while it was performing some blocking operation (e.g. interruptible IO, wait, sleep)
thread.sleep(long millis) doesn't work in Android
It is not "not working" - but it might throw an InterruptedException that you either have to declare to throw too (throws InterruptedException in the method signature), or you should catch it:
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
PS: sleep is a static method and should be called in a static way: Thread.sleep instead of thread.sleep
Related Topics
Calling Static Method from Another Java Class
Jframe.Dispose() VS System.Exit()
How to Intercept a Method Invocation with Standard Java Features (No Aspectj etc)
How to Set Same Scale for Domain and Range Axes Jfreechart
Xml Element with Attribute and Content Using Jaxb
Display Mouse Coordinates Near Mouse as Hints on Mouse Move
Localdate to Java.Util.Date and Vice Versa Simplest Conversion
Jaxb Creating Context and Marshallers Cost
JSON - Iterate Through JSONarray
Read Rsa Private Key of Format Pkcs1 in Java
Jackson: How to Add Custom Property to the JSON Without Modifying the Pojo
Why Are New Java.Util.Arrays Methods in Java 8 Not Overloaded for All the Primitive Types
Gson.Tojson() Throws Stackoverflowerror
Calling Setters from a Constructor