B
Ben_
Hello,
I've not found myself very lucky in finding a good article telling what to
do with the InterruptedException...
I'd like is read some discussions of best coding practices on that
particular field.
Many pieces of code (and not only samples) read like this:
(1)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
// Variations of comments like "don't care", "do nothing"
// or log meaningless message to System.out, etc
}
A variation seen consist in wrapping and re-throwing the exception:
(2)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
throw new RuntimeException(e);
}
Another approach seen consists in adding a "throws InterruptedException" to
the method signature:
(3)
public void doSomething() throws InterruptedException {}
In the case of a run() method, I can understand that nothing particular
needs to be done:
public void run() {
try {
while(!Thread.interrupted()) {
Thread.sleep(500);
// do something
}
} catch(InterruptedException e) {
// OK to terminate this way.
}
}
But for other cases, where a piece of code has to simply wait for some time
before doing something ?
Thaughts or pointers appreciated.
Thx.
I've not found myself very lucky in finding a good article telling what to
do with the InterruptedException...
I'd like is read some discussions of best coding practices on that
particular field.
Many pieces of code (and not only samples) read like this:
(1)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
// Variations of comments like "don't care", "do nothing"
// or log meaningless message to System.out, etc
}
A variation seen consist in wrapping and re-throwing the exception:
(2)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
throw new RuntimeException(e);
}
Another approach seen consists in adding a "throws InterruptedException" to
the method signature:
(3)
public void doSomething() throws InterruptedException {}
In the case of a run() method, I can understand that nothing particular
needs to be done:
public void run() {
try {
while(!Thread.interrupted()) {
Thread.sleep(500);
// do something
}
} catch(InterruptedException e) {
// OK to terminate this way.
}
}
But for other cases, where a piece of code has to simply wait for some time
before doing something ?
Thaughts or pointers appreciated.
Thx.