e(fx)clipse 1.2.0 – Helper API to call methods who throw exceptions


Have you ever worked with a library who throws an exceptions but there’s not possible recovery for you and you simply want to move on with a default value or null?

I’ve had the “fun” to work with something like this lately and all the exception handling cluttered my code. I’m using the IMarker-API as an example

private Annotation transform(IMarker m) {
   String type;
   try {
      type = marker.getType();
   } catch(CoreException e) {
      type = "Unknown";
   }
   
   return new Annotation(type,....);
}

In the ideal case IMarker would have similar as it has for getAttribute() a method who allows to define a default instead of throwing the exception but apparently this not the case and could only be fixed when adopting Java8.

So I’ve add a set of helper methods to deal with APIs who throw exceptions and now I can write:

import static org.eclipse.fx.core.function.ExExecutor.*;

private Annotation transform(IMarker m) {
  return new Annotation(
    executeSupplierOrDefault(m::getType, e -> "Unknown"), .....);
}
This entry was posted in e(fx)clipse. Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.