39 lines
920 B
Plaintext
39 lines
920 B
Plaintext
== Why is this an issue?
|
|
|
|
In applications where the accepted practice is to log an ``++Exception++`` and then rethrow it, you end up with miles-long logs that contain multiple instances of the same exception. In multi-threaded applications debugging this type of log can be particularly hellish because messages from other threads will be interwoven with the repetitions of the logged-and-thrown ``++Exception++``. Instead, exceptions should be either logged or rethrown, not both.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
catch (SQLException e) {
|
|
...
|
|
LOGGER.log(Level.ERROR, contextInfo, e);
|
|
throw new MySQLException(contextInfo, e);
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
catch (SQLException e) {
|
|
...
|
|
throw new MySQLException(contextInfo, e);
|
|
}
|
|
----
|
|
or
|
|
|
|
[source,text]
|
|
----
|
|
catch (SQLException e) {
|
|
...
|
|
LOGGER.log(Level.ERROR, contextInfo, e);
|
|
// handle exception...
|
|
}
|
|
----
|
|
|
|
|