Showing posts with label exceptions. Show all posts
Showing posts with label exceptions. Show all posts

Sunday, November 5, 2017

Apache Commons ContextedException

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

}



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)





Saturday, July 1, 2017

OverOps "The Complete Guide to Solving Java Application Errors in Production"

http://land.overops.com/java-application-errors/

I have downloaded the interesting free PDF. In a nutshell:

NullPointerException are the most common type of exception - followed by NumberFormatException (IMHO both can be avoided by better checks of input data/parameters and configuration: the external world is your enemy, never trust it)

Pareto Law: 97% of error logs are caused by just 3% unique errors

Read Joshua Bloch "Effective Java 2nd Edition" on how to handle exceptions

Logging errors is expensive in term of disk space... it's cheaper to fix them than to ignore them!

Operational noise is a killer: make sure your logs are clean, and your exception contain all the context information necessary to troubleshoot them



Sunday, April 3, 2016

ExceptionInInitializerError , NoClassDefFoundError and ClassNotFoundException

https://docs.oracle.com/javase/7/docs/api/java/lang/ExceptionInInitializerError.html
"an exception occurred during evaluation of a static initializer or the initializer for a static variable."

https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html
"no definition of the class could be found"

the above 2 are LinkageError (Error). The one below is an Exception:
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
Thrown when an application tries to load in a class through its string name using:

    The forName method in class Class.
    The findSystemClass method in class ClassLoader .
    The loadClass method in class ClassLoader. 

but no definition for the class with the specified name could be found. 

Difference between NoClassDefFoundError and ClassNotFoundException is well explained here:
http://www.javaroots.com/2013/02/classnotfoundexception-vs.html "This is thrown when at compile time the required classes are present, but at run time the classes are changed or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath, but one of the classes which are required by this class are either removed or failed to load by compiler. So you should see the classes which are dependent on this class."

Examples: this will throw a
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
 at com.pierre.pvtest.Test.(Test.java:4)



package com.pierre.pvtest;

public class Test {
 static int a = 1 / 0;
 public static void main(String[] args) {
  System.out.println("hello");
 }
}



If I compile this:
package com.pierre.pvtest;

public class Test {
 
 public static void main(String[] args) {
  Test1 test1 = new Test1();
  test1.hello();
  System.out.println("hello");
 }
}

package com.pierre.pvtest;

public class Test1 {
 public void hello() {
  System.out.println("Test1 hello");
 }

}

then I delete Test1.class, I get this:
java com.pierre.pvtest.Test
Exception in thread "main" java.lang.NoClassDefFoundError: com/pierre/pvtest/Test1
        at com.pierre.pvtest.Test.main(Test.java:6)
Caused by: java.lang.ClassNotFoundException: com.pierre.pvtest.Test1
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more