• 0.26.0

Logging Exceptions Thrown out of Java Methods and Swallowing Them

Annotate your methods with @Quietly annotation and every exception thrown out of the method will not be propagated, but will be logged through SLF4J:

public class Resource {
  private Object content;
  @Quietly
  public void load(URL url) {
    this.content = url.openConnection().getContent();
  }
}

If an exception occurs, it will not be thrown to the caller and it will be logged through SLF4J logging facility.

Also, take a look at @LogExceptions annotation. It does the same, but it propagates exceptions thrown to the caller.

Methods annotated with @Quietly must strictly have a void return type; otherwise, an exception will be thrown at runtime when the method is invoked. If you wish to check for non-compliant methods at compile time, you may incorporate the annotation processor QuietlyReturnTypeProcessor during your build. An example usage with maven-processor-plugin is:

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
        <processors>
          <processor>com.jcabi.aspects.apt.QuietlyReturnTypeProcessor</processor>
        </processors>
      </configuration>
    </execution>
  </executions>
</plugin>

The mechanism is implemented with AOP/AspectJ. Read to know how to integrate it into your pom.xml.