Developers often forget to attach context information to their exceptions. So one gets messages like "Unable to connect to server" and we don't know to which server and port the connection was attempted.
Add this to your pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> </dependencies>
and this is the test code
package pvexceptions; import org.apache.commons.lang3.exception.ContextedException;; public class PVExceptionGenerator { public static void main(String[] args) { try { try { int a = 1 / 0; } catch (Throwable a) { throw new ContextedException("something went wrong in division", a).addContextValue("customer", "Mario").addContextValue("custno", 1234); } } catch (Throwable t) { t.printStackTrace(); } } }
The output is:
org.apache.commons.lang3.exception.ContextedException: something went wrong in division
Exception Context:
[1:customer=Mario]
[2:custno=1234]
---------------------------------
at pvexceptions.PVExceptionGenerator.main(PVExceptionGenerator.java:11)
Caused by: java.lang.ArithmeticException: / by zero
at pvexceptions.PVExceptionGenerator.main(PVExceptionGenerator.java:9)
No comments:
Post a Comment