69 lines
1.9 KiB
Plaintext
69 lines
1.9 KiB
Plaintext
== Why is this an issue?
|
|
|
|
``++InterruptedExceptions++`` should never be ignored in the code, and simply logging the exception counts in this case as "ignoring". The throwing of the ``++InterruptedException++`` clears the interrupted state of the Thread, so if the exception is not handled properly the information that the thread was interrupted will be lost. Instead, ``++InterruptedExceptions++`` should either be rethrown - immediately or after cleaning up the method's state - or the thread should be re-interrupted by calling ``++Thread.interrupt()++`` even if this is supposed to be a single-threaded application. Any other course of action risks delaying thread shutdown and loses the information that the thread was interrupted - probably without finishing its task.
|
|
|
|
|
|
Similarly, the ``++ThreadDeath++`` exception should also be propagated. According to its JavaDoc:
|
|
|
|
____
|
|
If ``++ThreadDeath++`` is caught by a method, it is important that it be rethrown so that the thread actually dies.
|
|
|
|
____
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public void run () {
|
|
try {
|
|
while (true) {
|
|
// do stuff
|
|
}
|
|
}catch (InterruptedException e) { // Noncompliant; logging is not enough
|
|
LOGGER.log(Level.WARN, "Interrupted!", e);
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public void run () {
|
|
try {
|
|
while (true) {
|
|
// do stuff
|
|
}
|
|
}catch (InterruptedException e) {
|
|
LOGGER.log(Level.WARN, "Interrupted!", e);
|
|
// Restore interrupted state...
|
|
Thread.currentThread().interrupt();
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://cwe.mitre.org/data/definitions/391[MITRE, CWE-391] - Unchecked Error Condition
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|