How to exclude a line from jacoco code coverage?

As for now, there's no ability to exclude a specific line (see the link):

As of today JaCoCo core only works on class files, there is no source processing. This would require a major rework of the architecture and adds additional configuration hassles.

It means, Jacoco analyzes byte code of your program, not your sources, as the result it cannot use hints like comments.

Follow the corresponding issue to track the status of such feature implementation.

As a workaround you can put it into a separate method, but see, it's a bad smell when you change your code just to reach 100% coverage level.


I suspect what you're really aiming for is 100% coverage. Consider re-writing the code using a try-with-resources block instead. For example:

try (final InputStream inputStream = new FileInputStream(file)){
    //work with inputStream; auto-closes
}
catch (final Exception ex){
    //handle it appropriately
}