0

If Thread.sleep(3000); code is given, it throws an error as

"Unhandled exception type Interrupted Exception".

Mouse hover to the error, it is displaying as "Surround with try/catch". By double clicking it i am getting as below and wworking fine. Kindly say me the difference of both.

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
0

Thread.sleep(ms) throws an exception as you are trying to interuption in thread execution

It may throw InterruptedException which directly extends java.lang.Exception so you have to catch it or declare to throw it.

so that we have to use the try-catch

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {

    e.printStackTrace();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.