Quantcast
Channel: Java mon amour
Viewing all articles
Browse latest Browse all 1124

Apache Commons ContextedException

$
0
0
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/exception/ContextedException.html

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();
}
}

}





Viewing all articles
Browse latest Browse all 1124

Trending Articles