Closed GoogleCodeExporter closed 9 years ago
We tend to view "log and rethrow" as an antipattern: Either handle the
exception, or throw. If you throw, use exception chaining. That way, the
eventual handler has all the context in one place rather than split across
multiple log messages.
But I'm not sure I've ever heard someone make a case for "log and rethrow."
Want to give it a shot?
Original comment by cpov...@google.com
on 14 Mar 2014 at 2:07
I understand that you should either act (log) or rethrow. And I completely
agree with that.
However, we're not in an all-pinky world and not all the exception handling is
perfect in a project and if later on my exception is swallowed by some code I
didn't write and yet we have an issue, I still want to be able to trace where
it originated from. Using the flow tracing helps doing that.
Actually, my logger is configured to print the full stack trace only in WARN
and higher. (also, "throwing" has been reduced from ERROR to TRACE)
So for tracing purposes, I should be able to see when my execution enters a
method and when it leaves it. Using propagate() bypasses my tracing.
Original comment by ogregoire
on 14 Mar 2014 at 2:43
I would suggest a custom method:
throw wrapAndLog(logger, e);
It's less code, and it saves us from worrying about whether the inclusion of a
wrap() method will encourage misuse.
Original comment by cpov...@google.com
on 14 Mar 2014 at 3:36
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:09
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:07
Original issue reported on code.google.com by
ogregoire
on 14 Mar 2014 at 11:20